How to avoid “NSInternalInconsistencyException” in iPhone?

被刻印的时光 ゝ 提交于 2019-12-25 02:46:21

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!