Parse.com查询用户类(swift)

时间:2014-10-02 20:08:45

标签: swift parse-platform pfquery

当我尝试从“User”类查询所有用户时遇到一些奇怪的问题它找不到任何用户

var query:PFQuery=PFQuery(className: "User");
    query.findObjectsInBackgroundWithBlock {
        (objects: [AnyObject]!, error: NSError!) -> Void in
        if !(error != nil) {
            for(var i=0;i<objects.count;i++){
                var object=objects[i] as PFObject;
                var name = object.objectForKey("username") as String;
                println(name);

            }
        }
    }

我试过替换

 var query:PFQuery=PFQuery(className: "User");

var query:PFQuery=PFQuery(className: "AnotherClass");

一切都像我应该的那样。我认为“用户”类

一定是特别的

4 个答案:

答案 0 :(得分:23)

用户不是User表的相应名称。您需要使用以下内容:

var query : PFQuery = PFQuery(className: "_User")

更合适的方法是:

var query : PFQuery = PFUser.query()

答案 1 :(得分:5)

您无法查询此类用户。 相反,你需要:

var findUsers:PFQuery = PFUser.query();
findUsers.whereKey("username",  equalTo: searchText.text)

请参阅解析文档:

https://parse.com/docs/ios_guide#users-querying/iOS

答案 2 :(得分:0)

我也被困在这段代码上了。我能够使用以下代码查询用户及其数据:

    var usernames = [String]()
    var userImages = [Data]()

    var query : PFQuery = PFUser.query()!
                    // Interested in locations near user.
                    query.whereKey("location", nearGeoPoint: geopoint!)
                    // Limit what could be a lot of points.
                    query.limit = 10
                    do {
                        let queryObjects = try query.findObjects()
                        for object in queryObjects {
                            self.usernames.append(object["username"] as! String)
                            self.userImages.append(object["image"] as! Data)
                        }
                    } catch {
                        print("Error")
                    }

答案 3 :(得分:-1)

试试这个......

 var ObjectIDQuery = PFQuery (className: "User")
    ObjectIDQuery.findObjectsInBackgroundWithBlock({
        (objectsArray : [AnyObject]?, error: NSError?) -> Void in



        var objectIDs = objectsArray as! [PFObject]

        NSLog("\(objectIDs)")

        for i in 0...objectIDs.count-1{
            self.NameArray.append(objectIDs[i].valueForKey("username")as!  String)


            self.iDArry.append(objectIDs[i].valueForKey("objectId") as!  String)

            self.tableView.reloadData()