ios - iOS如何从解析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
}
}
但我希望每个变量都会获取每一行。
更新:
来自答案的代码:
解决方案
'!' 不要在展开时使用它。显然,您将拥有一个名称,该名称在您的 for 循环中也是最后一个。
你应该学习 Codables 在 Swift 4 中解析 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 ")
}
}
推荐阅读
- python - For-loop-nested-in-for-loop 通过 csv.reader() 但外循环只经过一次?
- haskell - 使用列表解决方案进行优化:编译器错误
- rest - Loopback 4 扩展 REST 模型端点
- javascript - Cookies.set 没有在 cookie (js-cookie) 中设置值
- python-3.x - 有没有办法获得numpy数组中非零元素的总和?我不断收到 TypeError
- ios - 什么是通过刷新 id-token 的正确方法。AWSMobileClient 迅速?
- r - R:如何检查未命名列表的所有值是否为 TRUE?
- azure - 当用户未在 Azure AD B2C 中注册时如何禁用验证码电子邮件
- github - 我可以使用 GitHub GraphQL API 在特定存储库中搜索文件的路径吗?
- react-native - 更新当前位置时,Google API 会触发多次吗?