Reputation: 25
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'
Upvotes: 2
Views: 879
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
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
Reputation: 57124
objects
is of type [PFObject]?
- an optional array of PFObject
s.
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