iOS -[__NSArrayI addObject:]: unrecognized selector sent to instance~解决方法
//联系人:石虎
QQ: 1224614774昵称:嗡嘛呢叭咪哄
报错: 一
"*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]"
*** Terminating app due to uncaught exception "NSInvalidArgumentException", reason: "-[__NSArrayI addObject:]: unrecognized selector sent to instance 0x4b17be0"
错误:NSArray 不能使用 addObject:方法。可能是在程序运行的过程中,NSMutableArray转为了NSArray。
解决方案:核查数组,看看是否在NSArray 之后使用了addObject:方法 。
报错: 三
当我创建了一个NSMutableArray 对象的时候
@property (nonatomic,copy)NSMutableArray *children;
然后通过addObject运行就会报错,[__NSArrayI addObject:]: unrecognized selector sent to instance
解决方式:
1. 在 ARC 环境下, 将 dataSource 修饰词 改为 strong
2、通过理解,我们知道addObject后的array其实是变了,可能内存变大了,你可以理解成这个对象已经不是原来的了,就相当于没有定义一个具体实例对象一样。但通过@synthesize 默认的setter不能保证copy就一定等于mutableCopy;所以我们需要自定义自己的setter方法。
例如我的定义,在.m中增加我们自己定义的setter方法,让它进行mutableCopy即可。
-(void)setChildren:(NSMutableArray*) array{
if(children != nil)
{
children = nil;
}
children = [array mutableCopy];
}
3、把copy变成retain,因为不是copy创建新内存和新赋值的话,原有实例对象就不会“全新”导致unrecognized selector sent to instance。
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。
- 上一篇: iOS 数组中replaceObjectAtIndex方法
- 下一篇: iOS iPhone X 适配