对Parse的查询无效

时间:2015-10-26 16:51:34

标签: ios swift parse-platform

这给了我一个错误。我附上了错误的截图。 enter image description here

class UserTableViewController: UITableViewController {
    var users = [""]

    override func viewDidLoad() {
        super.viewDidLoad()

        //Printing the currentUser name
        print(PFUser.currentUser()!)

        var query = PFUser.query()

        query!.findObjectsInBackgroundWithBlock({ (objects : [AnyObject]!, error : NSError!) -> Void in
            self.users.removeAll(keepCapacity : true)

            for object in objects {
                var user : PFUser = object as PFUser

                self.users.append(user.username)
            }
        })
    }

1 个答案:

答案 0 :(得分:2)

问题似乎在于您如何声明users数组。试试这个。

class UserTableViewController: UITableViewController {
    var users = [String]()

    override func viewDidLoad() {
        super.viewDidLoad()

        //Printing the currentUser name
        print(PFUser.currentUser()!)

        var query = PFUser.query()

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

            if let parseUsers = objects as? [PFUser] {
                for u in parseUsers {
                    //parseUsers is now an array of PFUser
                    self.users.append(u.username)
                }
            } 
        })
    }

此外,您无法像这样投射您的用户。试试这个:

if let parseUsers = objects as? [PFUser] {
    //parseUsers is now an array of PFUser
}