iOS 如何从解析中获取行 Swift 4
iOS How to fetch rows from parse Swift 4
我有一个问题,我想从解析中获取五行,当我打印数据时,我获取它是正常的,当我在变量中加载数据时,在我的变量中只保存一行:
我的代码:
let query = PFQuery(className: "changeovers")
query.addDescendingOrder("time_downtime1")
query.limit = 5
query.findObjectsInBackground { (objects, error) in
for object in objects! {
self. machineNameOne = object["stantionName"] as! String
self. machineNameTwo = object["stantionName"] as! String
self. machineNameThree = object["stantionName"] as! String
self. machineNameFour = object["stantionName"] as! String
self. machineNameFive = object["stantionName"] as! String
}
}
Console
但我希望每个变量都能获取每一行。
更新:
答案中的代码:
GIF CONSOLE
'!'不要在展开时使用它。显然,您将在 for 循环中使用最后一个名称。
您应该在 Swift 4.
中学习 Codables to Parse JSON
现在你应该这样做:
let query = PFQuery(className: "changeovers")
query.addDescendingOrder("time_downtime1")
query.limit = 5
var stationNames = [String]()
query.findObjectsInBackground { (objects, error) in
if let objectArray = objects {
for object in objectArray {
if let stationName = object["stationName"] as? String {
stationNames.append(stationName)
} else {
print("stationName is not present ")
}
}
print("Station names : \(stationNames)")
} else {
print("objects is nil ")
}
}
我有一个问题,我想从解析中获取五行,当我打印数据时,我获取它是正常的,当我在变量中加载数据时,在我的变量中只保存一行:
我的代码:
let query = PFQuery(className: "changeovers")
query.addDescendingOrder("time_downtime1")
query.limit = 5
query.findObjectsInBackground { (objects, error) in
for object in objects! {
self. machineNameOne = object["stantionName"] as! String
self. machineNameTwo = object["stantionName"] as! String
self. machineNameThree = object["stantionName"] as! String
self. machineNameFour = object["stantionName"] as! String
self. machineNameFive = object["stantionName"] as! String
}
}
Console
但我希望每个变量都能获取每一行。
更新:
答案中的代码:
GIF CONSOLE
'!'不要在展开时使用它。显然,您将在 for 循环中使用最后一个名称。
您应该在 Swift 4.
中学习 Codables to Parse JSON现在你应该这样做:
let query = PFQuery(className: "changeovers")
query.addDescendingOrder("time_downtime1")
query.limit = 5
var stationNames = [String]()
query.findObjectsInBackground { (objects, error) in
if let objectArray = objects {
for object in objectArray {
if let stationName = object["stationName"] as? String {
stationNames.append(stationName)
} else {
print("stationName is not present ")
}
}
print("Station names : \(stationNames)")
} else {
print("objects is nil ")
}
}