-1

i am trying to sort an nsarray of heroes (objects). Each hero is of that kind

@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

To sort an array of these heroes i wrote a sortfunction with two descriptors

-(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];
}

Unfotunately it is crashing while sorting an array of 5 objects when calling sortedArrayUsingDescriptors with

 *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString bonus]: unrecognized selector sent to instance 0x9674520'

I have no idea what is going wrong. Does anybody can point me on my error?

------- self solution ----------- Hey thanks for pointing to the solution. Here is my correct code for other people having same problem.

@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];

        ...
    }];
    ...
}
4

1 回答 1

1

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

于 2013-10-07T19:42:32.023 回答