Reputation: 1521
i hope you all doing great :)
my code here retrieve data from classname score, the data is in numbers. what i'm trying to do is to grab all score values for a particular player and count the average score to be displayed. i should do it with arrays but i do not know how, i need someone to guide me. Thank you in advance
var info = PFQuery(className: "score")
info.whereKey("player_name", equalTo: player!)
info.findObjectsInBackgroundWithBlock{
(objects, error) -> Void in
if error == nil {
for me in objects! {
var mine = me.objectForKey("score")
print(score)
}
}else {
print(error)
}
}
}
Upvotes: 0
Views: 274
Reputation: 1521
finally i've found the answer, in case anyone wonder how
var sum = 0
for me in objects! {
sum += me.objectForKey("score") as! Int
var number = info.countObjects()
var average = sum / number
print(average)
}
Upvotes: 0
Reputation: 791
This should work. Just finding the average the old fashioned way.
var sum = 0
for me in objects! {
sum += me.objectForKey("score") as! Int
}
let avg : Double = Double(sum) / Double(numbers.count)`
Upvotes: 0
Reputation: 285072
It should work with the key value coding collection operator @avg
, just
let averageScore = objects!.valueForKeyPath("@avg.score")
no repeat loop needed.
Upvotes: 2