1
votes

NSMutableArray *labels; and it is properly populated.

    NSSortDescriptor* sortOrder = [NSSortDescriptor sortDescriptorWithKey: @"self" ascending: NO];
labels = [labels sortedArrayUsingDescriptors: [NSArray arrayWithObject: sortOrder]];    

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI addObject:]: unrecognized selector sent to instance.

Even if I change NSArray to NSMutuableArray in the last line, I still get the error.

Thanks

2
Why are you using "self" as the sort key?Ben Zotto
To be honest with you, I just copied that code and I am trying to understand this whole sort mechanism.saman01
I understand it now. Self was refering to the label I want to sort. But should have been using self.text.saman01

2 Answers

3
votes

dont assign the same array to it.if it is nsarray which is immutable.

so do like this

put ur

   labels as NSMutableArray


NSMutableArray *resultArray;

NSSortDescriptor* sortOrder = [NSSortDescriptor sortDescriptorWithKey: @"self" ascending: NO];


resultArray = [[labels sortedArrayUsingDescriptors: [NSArray arrayWithObject:sortOrder]] mutableCopy];    

self.labels=resultArray;//updated code

NSLog(@"resultArray : %@ \n\n",resultArray);
1
votes
NSArray * descriptors = [NSArray arrayWithObjects: sortOrder, nil];
labels = [labels sortedArrayUsingDescriptors:descriptors];

This should do the trick for you.