今天在操作NSMutableArray数字增加和删除时候出现崩溃,报错[__NSArrayI addObjectsFromArray:]: unrecognized selector。
找了一个小时,发“__NSArrayI”这个提示比较重要。查了一下发现
__NSArrayI
is a code-word for an immutable array - that is, a "regular" NSArray
which you cannot change.
__NSArrayM
is a code-word for a mutable array - that is, NSMutableArray
. In NSMutableArray
, you can add and remove items.
NSArrayM才是NSMutableArray.
奇怪,我明明是声明的是NSMutableArray. 怎么突然变成了NSArray.
排查代码才发现,在拷贝数组时候用了 .copy 来拷贝,而NSMutableArray拷贝数组时候需要用 .mutableCopy !!!