gpt4 book ai didi

objective-c - copy、mutableCopy 和赋值有什么区别?

转载 作者:行者123 更新时间:2023-12-02 04:21:45 24 4
gpt4 key购买 nike

我以为我理解了 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 之一发生相同的更改?

最佳答案

// Can't modify this NOW
NSArray *arrayB = [NSArray arrayWithObject:@"a"];

// Create another copy of Array B like XEROX. If you change in XEROX
// it does not reflect to original Copy
NSArray *arrayA = [arrayB copy];

// This is a mutable copy so you can make changes in this and you have
// to change NSArray to NSMutableArray here as it the mutable copy.
NSMutableArray *arrayC = [arrayB mutableCopy];

// Any change made in arrayD reflect to arrayB but arrayD is NSArray
// (immutable) so you can not make any changes in arrayD
NSArray *arrayD = arrayB;

希望这能帮助您解决问题。

关于objective-c - copy、mutableCopy 和赋值有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29938947/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com