2

I plan to retrieve the score of my current user. Here is my code. Thanks in advance.

let query = PFQuery(className: "UserData")

query.whereKey("user", equalTo: "cuWkby3Fm0")
    query.findObjectsInBackgroundWithBlock {
    (objects: [PFObject]?, error: NSError?) -> Void in

if error == nil {
    print("successfully retrieved \(objects!.score) ")
} else {
    print("Error: \(error!) \(error!.userInfo)")
}

The error is

Value of type '[PFObject]' has no member 'score'

Parse class

2
  • Please post code, not screenshots of code. Include the error message as text and maybe a screenshot of the message if needed. Commented Jan 7, 2016 at 20:33
  • general idea: objects![0]["score"] Commented Jan 7, 2016 at 20:33

3 Answers 3

4

objects is of type [PFObject]? - an optional array of PFObjects.

You probably want to get the score of some of the contained elements. For example:

if let first = objects.first {
    print(first["score"])
}
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, and another mistake i made is that I passed a string to the user. Should be an object. Thanks!
3

If you want the score of your current user, then query for PFUser.currentUser() instead of a hard-coded string. Also, make sure you're actually working with a single object instead of multiple objects.

let query = PFQuery(className: "UserData")

query.whereKey("user", equalTo: PFUser.currentUser())
query.findObjectsInBackgroundWithBlock {
(objects: [PFObject]?, error: NSError?) -> Void in

if error == nil {
    print("successfully retrieved \(objects!.first.score) ")
} else {
    print("Error: \(error!.localizedDescription)")
}

Comments

1

If you want to get all gameScores for specific user. You should iterate your data with for in loop.

For example.

for object in objects {
    print(object["score"])
}

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.