我有以下代码:
self.itemsCopy = [self.items mutableCopy];
//[self.itemsCopy addObjectsFromArray:self.items];
NSLog(@"------- BEFORE APPEND --------");
NSLog(@"items count: %d",[items count]);
NSLog(@"itemsCopy count: %d",[itemsCopy count]);
我的结果是:
------- BEFORE APPEND --------
items count: 15
itemsCopy count: 15
但是,如果我将第一行从mutableCopy更改为addObjectsFromArray:
[self.itemsCopy addObjectsFromArray:self.items];
我的新结果是:
------- BEFORE APPEND --------
items count: 15
itemsCopy count: 0
为什么mutableCopy会填充itemsCopy,但是addObjectsFromArray却没有?
答案 0 :(得分:6)
因为你错过了用NSMutableArray初始化它,所以self.itemsCopy是nil。