saman01
saman01

Reputation: 1004

NSMutuableArray sort problem

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

Upvotes: 2

Views: 1118

Answers (2)

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);

Upvotes: 3

Praveen S
Praveen S

Reputation: 10393

NSArray * descriptors = [NSArray arrayWithObjects: sortOrder, nil];
labels = [labels sortedArrayUsingDescriptors:descriptors];

This should do the trick for you.

Upvotes: 1

Related Questions