0

I'm trying to run a query to have the best 10 player with a high score in an array, I get nil when I run this query, what am I doing wrong ? please help

var userNames = NSArray!()
var genders = NSArray!()

let query = PFQuery(className: "_User")
query.selectKeys(["topscoreuser", "username"])
query.orderByDescending("topscoreuser")
query.limit = 10
query.findObjectsInBackgroundWithBlock { (objects: [AnyObject]?, error: NSError?) in
    if error == nil {
        userNames = PFUser.currentUser()?.objectForKey("username") as? NSArray
        genders =   PFUser.currentUser()?.objectForKey("topscoreuser") as? NSArray
        print("\(userNames)")
        print("\(genders)")
    } else{
        //fail
    }
}

1 Answer 1

1

So you're doing a query to get some objects, and then ignoring them. You're also using the private User class name, which you shouldn't. You can use KVC to interrogate the objects:

var userNames = NSArray!()
var genders = NSArray!()

let query = PFUser.query()
query.selectKeys(["topscoreuser", "username"])
query.orderByDescending("topscoreuser")
query.limit = 10
query.findObjectsInBackgroundWithBlock { (objects: [AnyObject]?, error: NSError?) in
    if error == nil {
        userNames = objects?.valueForKey("username") as? NSArray
        genders =   objects?.valueForKey("topscoreuser") as? NSArray
        print("\(userNames)")
        print("\(genders)")
    } else{
        //fail
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.