使用多个描述符对对象的nsarray进行排序

时间:2013-10-07 19:38:38

标签: ios objective-c sorting nsarray descriptor

我正在尝试对一群英雄(物体)进行排序。每个英雄都是那种

@interface Hero : NSObject <NSCoding>

@property (strong,nonatomic) NSString *name;
@property (strong,nonatomic) NSString *correctAnswers;
@property (strong,nonatomic) NSString *falseAnswers;
@property (strong,nonatomic) NSString *bonus;
@property (strong,nonatomic) NSString *level;

-(NSInteger*) inLevel;

+(Hero*) createHero:(NSString*)name
            inLevel:(NSString*)level
          withBonus:(NSString*)bonus
     correctAnswers:(NSString*)correctAnswers
       andFalseOnes:(NSString*)falseAnswers;

- (NSString *)description;

@end

为了对这些英雄的数组进行排序,我编写了一个带有两个描述符的分类函数

-(void) sortHeros {
    NSSortDescriptor *level = [NSSortDescriptor sortDescriptorWithKey:@"level" ascending:YES comparator:^NSComparisonResult(id a, id b) {
        Hero *hero1 = a;
        Hero *hero2 = b;

        int levelHero1 = [hero1.level intValue];
        int levelHero2 = [hero2.level intValue];

        if (levelHero1 > levelHero2) {
            return (NSComparisonResult)NSOrderedAscending;
        } else if (levelHero1 < levelHero2) {
            return (NSComparisonResult)NSOrderedDescending;
        }

        return (NSComparisonResult)NSOrderedSame;
    }];

    NSSortDescriptor *bonus = [NSSortDescriptor sortDescriptorWithKey:@"bonus" ascending:YES comparator:^NSComparisonResult(id a, id b) {
        Hero *hero1 = a;
        Hero *hero2 = b;

        int bonusHero1 = [hero1.bonus intValue];
        int bonusHero2 = [hero2.bonus intValue];

        if (bonusHero1 > bonusHero2) {
            return (NSComparisonResult)NSOrderedAscending;
        } else if (bonusHero1 < bonusHero2) {
            return (NSComparisonResult)NSOrderedDescending;
        }

        return (NSComparisonResult)NSOrderedSame;
    }];

    NSArray *sortedArray;
    sortedArray = [self.heros sortedArrayUsingDescriptors:[NSArray arrayWithObjects:level, bonus, nil]];

    self.heros = [sortedArray mutableCopy];
}

不幸的是,在使用

调用sortedArrayUsingDescriptors时排序5个对象的数组时崩溃了
 *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString bonus]: unrecognized selector sent to instance 0x9674520'

我不知道出了什么问题。有人能指出我的错误吗?

-------自我解决方案----------- 嘿,谢谢你指出解决方案。这是我对其他有同样问题的人的正确代码。

@interface Hero ...
-(NSString*) heroBonus;
-(NSString*) heroLevel;
@end

-(void) sortHeros {
    NSSortDescriptor *level = [NSSortDescriptor sortDescriptorWithKey:@"heroLevel" ascending:NO comparator:^NSComparisonResult(id a, id b) {
        int levelHero1 = [a intValue];
        int levelHero2 = [b intValue];

        ...
    }];

    NSSortDescriptor *bonus = [NSSortDescriptor sortDescriptorWithKey:@"heroBonus" ascending:NO comparator:^NSComparisonResult(id a, id b) {
        int bonusHero1 = [a intValue];
        int bonusHero2 = [b intValue];

        ...
    }];
    ...
}

1 个答案:

答案 0 :(得分:1)

它告诉你,你发送密钥路径的类没有实现“奖励”的getter或方法。它还说“NSCFString,这意味着您将bonus keyPath发送到字符串对象而不是Hero对象。