首页 > 解决方案 > 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
    }
}

安慰

但我希望每个变量都会获取每一行。

更新:

来自答案的代码:

GIF 控制台

标签: iosswiftparse-platform

解决方案


'!' 不要在展开时使用它。显然,您将拥有一个名称,该名称在您的 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 ")
    }
}

推荐阅读