Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将对象附加到Swift 3中的数组_Swift - Fatal编程技术网

将对象附加到Swift 3中的数组

将对象附加到Swift 3中的数组,swift,Swift,尝试将我从Parse查询的对象推送到可以在UITableView中使用的数组时遇到了一些麻烦 这是我的密码 var locations = [AnyObject]() override func viewDidLoad() { super.viewDidLoad() // Query the Locations class. let query = PFQuery(className:"Location") query.findObjectsInBackgr

尝试将我从Parse查询的对象推送到可以在UITableView中使用的数组时遇到了一些麻烦

这是我的密码

var locations = [AnyObject]()

override func viewDidLoad() {
    super.viewDidLoad()

    // Query the Locations class.
    let query = PFQuery(className:"Location")

    query.findObjectsInBackground {
        (objects: [PFObject]?, error: Error?) -> Void in
        if error == nil {
            if let objects = objects {
                for object in objects {
                    self.locations.append(object)
                }
                self.venueTable.reloadData()
            }
        } else {
            // Log details of the failure
            print("Error: (error!) (error!.userInfo)")
        }
    }

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return locations.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let locationCell = tableView.dequeueReusableCell(withIdentifier: "locationCell", for: indexPath)

    let location = locations[indexPath.row]

    locationCell.textLabel?.text = location

    return locationCell
}

在for循环之后,locations中充满了解析数据,但当将其推送到
locationCell

时,不确定如何访问它。为locations设置的类型是[AnyObject],所以当您尝试设置labels text属性时,这将不起作用,因为它不是字符串

而是将其设置为[PFObject],然后使用PFObject的函数objectForKey从检索到的对象中获取相关字符串值

乙二醇


为位置设置的类型为[AnyObject],因此当您尝试设置labels text属性时,该类型无效,因为它不是字符串

而是将其设置为[PFObject],然后使用PFObject的函数objectForKey从检索到的对象中获取相关字符串值

乙二醇

var locations = [PFObject]()

override func viewDidLoad() {
    super.viewDidLoad()

    // Query the Locations class.
    let query = PFQuery(className:"Location")

    query.findObjectsInBackground {
    (objects: [PFObject]?, error: Error?) -> Void in
        if error == nil {
            if let objects = objects {

                self.locations = objects

                self.venueTable.reloadData()

            }

         } else {
        // Log details of the failure
        print("Error: (error!) (error!.userInfo)")
        }

    }

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

     return locations.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let locationCell = tableView.dequeueReusableCell(withIdentifier: "locationCell", for: indexPath)

    let location = locations[indexPath.row]

    locationCell.textLabel?.text = location.objectForKey("property name here") as? String

    return locationCell
}