我以为我理解了copy和mutableCopy之间的区别。但今天我怀疑自己。我还是不明白。
NSArray *arrayB = [NSArray arrayWithObject:@"a"];
NSArray *arrayA = [arrayB copy];
NSArray *arrayC = [arrayB mutableCopy];
NSArray *arrayD = arrayB;
当我对arrayA,arrayC或arrayD进行一些更改时,arrayB与更改的arrayA,arrayC和arrayD之间的更改是否相同?
答案 0 :(得分:3)
NSArray *arrayB = [NSArray arrayWithObject:@"a"]; // Can't modify this NOW
NSArray *arrayA = [arrayB copy]; // Create another copy of Array B like XEROX. If you change in XEROX it does not reflect to original Copy
NSArray *arrayC = [arrayB mutableCopy]; // This is just Mutable copy so you can make changes in this and you have to change NSArray to NSMutableArray here as it the mutable copy.
NSArray *arrayD = arrayB;// Any change made in arrayD reflect to ArrayB but ArrayD is NSArray (immutable) so you can not make any changes in ArrayD
希望这有助于为您解决问题。
答案 1 :(得分:0)
您无法对arrayA,arrayB或arrayD进行更改,它们是不可变的(并且arrayD不是新实例,它指向arrayB所执行的同一对象)。您只能将这些指针设置为指向其他数组。 如果你对arrayC(这里唯一可变的实例)进行了更改,那么不会,之前从中复制的arrayB将不会更改。