addObjectsFromArray与mutableCopy

时间:2010-07-01 16:58:51

标签: objective-c cocoa cocoa-touch arrays nsarray

我有以下代码:

    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却没有?

1 个答案:

答案 0 :(得分:6)

因为你错过了用NSMutableArray初始化它,所以self.itemsCopy是nil。