Error:* Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '* -[NSCFArray insertObject:atIndex:]: mutating method sent to immutable object'

今天在项目工程中想往可变数组中添加从服务器获取的元素,我用了一下方法:

[self.bannerArr addObjects:dic];

尝试几种方法后还是报错:

 reason: '-[__NSCFArray insertObject:atIndex:]: mutating method sent to immutable object'

打印 [self.dataArr class] 类型为 ___NSCFArray
查完资料总结原因如下:
因为这个变量之前被赋值了,而无法把一个可变的方法固执给一个不可变的对象。

最后解决办法:

 NSMutableArray *bannerArray = [[NSMutableArray alloc]init]; 
 [bannerArray addObjectsFromArray:self.bannerArr];
[bannerArray addObject:dic];
self.bannerArr = bannerArray;

记性差的还是做下记录为好

你可能感兴趣的:(Error:* Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '* -[NSCFArray insertObject:atIndex:]: mutating method sent to immutable object')