dksl176
dksl176

Reputation: 25

Parse Query in Swift using query.whereKey

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

Upvotes: 2

Views: 879

Answers (3)

Grant Park
Grant Park

Reputation: 1044

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)")
}

Upvotes: 3

emresancaktar
emresancaktar

Reputation: 1567

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"])
}

Upvotes: 1

luk2302
luk2302

Reputation: 57124

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"])
}

Upvotes: 4

Related Questions