ios - Sorting nsarray of objects with multiple descriptors -


i trying sort nsarray of heroes (objects). each hero of 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 array of these heroes wrote sortfunction 2 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 crashing while sorting array of 5 objects when calling sortedarrayusingdescriptors

 *** terminating app due uncaught exception 'nsinvalidargumentexception', reason: '-[__nscfstring bonus]: unrecognized selector sent instance 0x9674520' 

i have no idea going wrong. can point me on error?

------- self solution ----------- hey pointing solution. here correct code 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];          ...     }];     ... } 

it's telling class you're sending keypath doesn't implement getter or method "bonus". it's saying "nscfstring, meaning you're sending bonus keypath string object , not hero object.


Comments

Popular posts from this blog

c++ - CryptStringToBinary API behavior -

java.util.scanner - How to read and add only numbers to array from a text file -

iphone - Three second countdown in cocos2d -