问题
I am sorting an mutable array. For sorting I use:
NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"pubDate" ascending:NO];
[recent sortUsingDescriptors:[NSArray arrayWithObjects:descriptor, nil]];
recent1 = [recent sortedArrayUsingDescriptors:descriptor];
[descriptor release];
I am getting this error:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '*** -[NSCFArray replaceObjectAtIndex:withObject:]: mutating method sent to immutable object'
The line
recent1 = [recent sortedArrayUsingDescriptors:descriptor];
show warnings
- "passing argument 1 of 'sortedarrayusingdescritors' from distinct objective c type " and
- "assignment from distinct objective c type"
In my code, both recent
and recent1
are NSMutable
arrays. Where do I go wrong?
回答1:
recent1 = [recent sortedArrayUsingDescriptors:descriptor];
must be:
recent1 = [recent sortedArrayUsingDescriptors:[NSArray arrayWithObjects:descriptor, nil]];
Though I have no idea why you would want to sort an array that you already sorted with the same sort descriptors on the line directly above.
来源:https://stackoverflow.com/questions/2301835/how-to-avoid-nsinternalinconsistencyexception-in-iphone