开发者

NSMutuableArray sort problem

开发者 https://www.devze.com 2023-03-23 18:22 出处:网络
NSMutableArray *labels; and it is properly populated. NSSortDescriptor* sortOrder = [NSSortDescriptor sortDescriptorWithKey: @\"self\" ascending: NO];

NSMutableArray *labels; and it is properly populated.

    NSSortDescriptor* sortOrder = [NSSortDescriptor sortDescriptorWithKey: @"self" ascending: NO];
labels = [labels sortedArrayUsingDescriptors: [NSArray arrayWithObje开发者_Python百科ct: 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


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


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

This should do the trick for you.

0

精彩评论

暂无评论...
验证码 换一张
取 消