OStack程序员社区-中国程序员成长平台

标题: ios - 按数字排序解析对象 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 15:55
标题: ios - 按数字排序解析对象

我在 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]);
    }
}];



Best Answer-推荐答案


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/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4