ios - 按编号对解析对象进行排序

标签 ios objective-c cocoa-touch sorting parse-platform

我在 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/

相关文章:

iphone - 如何检查 heightForRowAtIndexPath 中的 UITableViewCell 是否不为零(已初始化)?

iphone - 限制 iPhone 中 UIButton 的可触摸区域?

ios - 如何在 JSON Alamofire 请求的函数中返回正确的值?

ios - ITMS 9000 : The binary you upload was invalid

iphone - 像 iBooks 应用程序一样的表格 View 标题 Logo

objective-c - 如何计算frame.size.width

ios - 以编程方式快速自动布局

objective-c - 如何在Cocoa中动态添加组件

objective-c - cocoa 中类簇和抽象工厂的区别

objective-c - Objective C 中的 CGContextFillPath 如何工作?