Swift Converting PFQuery to String Array for TableView

南笙酒味 提交于 2020-01-06 21:09:30

问题


I am trying to query all of the Parse users in my database and then display each individual user in their own cell in a tableview. I have set up my tableview, but I'm stuck on saving the user query to a string array that can be used within the tableview. I have created a loadParseData function that finds the objects in the background and then appends the objects queried to a string array. Unfortunately I am given an error message on the line where I append the data.

Implicit user of 'self' in closure; use 'self.' to make capture semantics explicit' This seems to me that it is suggestion that I use self. instead of usersArray. because this is within a closure, but I'm given another error if I run it that way, *classname* does not have a member named 'append'

Here is my code:

import UIKit

class SearchUsersRegistrationViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    var userArray = [String]()

    @IBOutlet var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.delegate = self
        tableView.dataSource = self

    }

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


    func loadParseData(){

        var query : PFQuery = PFUser.query()

        query.findObjectsInBackgroundWithBlock {
            (objects:[AnyObject]!, error:NSError!) -> Void in

            if error != nil{

                println("\(objects.count) users are listed")

                for object in objects {

                    userArray.append(object.userArray as String)

                }
            }

        }

    }


    let textCellIdentifier = "Cell"

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {

        return 1

    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        //return usersArray.count
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(textCellIdentifier, forIndexPath: indexPath) as SearchUsersRegistrationTableViewCell

        let row = indexPath.row

        //cell.userImage.image = UIImage(named: usersArray[row])

        //cell.usernameLabel?.text = usersArray[row]

        return cell
    }


}

回答1:


The problem is that userArray is an NSArray. NSArray is immutable, meaning it can't be changed. Therefore it doesn't have an append function. What you want is an NSMutableArray, which can be changed and has an addObject function.

var userArray:NSMutableArray = []

func loadParseData(){
    var query : PFQuery = PFUser.query()
    query.findObjectsInBackgroundWithBlock {
        (objects:[AnyObject]!, error:NSError!) -> Void in
        if error == nil {
            if let objects = objects {
                for object in objects {
                    self.userArray.addObject(object)
                }
            }
            self.tableView.reloadData()
        } else {
            println("There was an error")
        }
    }
}

Also, because objects are returned as 'AnyObject' you will have to cast them as PFUsers at some point in order to use them as such. Just something to keep in mind

For getting the user's username and displaying it

// Put this in cellForRowAtIndexPath

var user = userArray[indexPath.row] as! PFUser
var username = user.username as! String
cell.usernameLabel.text = username


来源:https://stackoverflow.com/questions/29812422/swift-converting-pfquery-to-string-array-for-tableview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!