我在 Parse 数据库中有一些对象,其中一些如下所示。我想按 highScore 对这些对象进行排序,它存储为数字。 (
"<Score: 0x7fee53740700, objectId: zMjL3eNWpI, localId: (null)> {\n Score = \"High Score: 60\";\n TeamName = \"Team0\";\n highScore = 60;\n}",
"<Score: 0x7fee534b5080, objectId: nysaJjYsth, localId: (null)> {\n Score = \"High Score: 86\";\n TeamName = Team1;\n highScore = 86;\n}",
"<Score: 0x7fee535f6ad0, objectId: 7Hj8RP4wYD, localId: (null)> {\n Score = \"High Score: 23\";\n TeamName = Team2;\n highScore = 23;\n}"
)
我有以下代码,我循环遍历对象并提取每个对象的 Number highScore,但我不知道如何继续。有没有办法返回 (NSComparisonResult)NSOrderedAscending ?如果有人有任何建议,请告诉我。谢谢。
PFQuery *query = [PFQuery queryWithClassName:self.parseClassName];//class name is Score
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
for (int i = 0; i<=objects.count; i++){
NSArray *array = [objects objectAtIndex:i];//Selects the first "object" from all the "objects"
NSNumber *test= [objects objectAtIndex:i];
array = [array valueForKey:@"highScore"];
test = [test valueForKey:@"highScore"];
test1 = [test intValue];//test1 is the current database object highScore for the current object
}
} else {
// Log details of the failure
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
}];
最佳答案
Parse.com 提供了一个很棒的 iOS SDK,它已经为您提供了您正在寻找的开箱即用的东西。当您创建 PFQuery
时,Parse 会为您提供按照您想要的方式对结果进行排序的选项。我相信你应该尝试这个:
PFQuery *query = [PFQuery queryWithClassName:self.parseClassName];
[query orderByAscending:@"highScore"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
for (PFObject *object in objects) {
NSLog(@"High Score is %d", [object["highScore"]intValue]);
}
} else {
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
}];
关于ios - 按编号对解析对象进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31763036/