淺拷貝: 淺拷貝並不拷貝對象自己, 只是拷貝指向對象的指針, 將指針指向新的對象;
深拷貝: 直接拷貝對象到內存中的一塊區域, 而後把新對象的指針指向這塊內存;
複製代碼
補充:html
在iOS中並非全部對象都支持Copy和MutableCopy;
遵循NSCopying協議的類能夠發送Copy協議;
遵循NSMutableCopying協議的類能夠發送MutableCopy消息.
若是一個對象沒有遵循這兩個協議而發送Copy或者MutableCopy消息,那麼會發生異常.
若是要遵循NSCopying協議, 那麼必須實現copyWithZone方法.
若是要遵循NSMutableCopying協議, 那麼必須實現mutableCopyWithZone方法;
複製代碼
//不可變字符串的內存地址和指針地址
NSString *test = @"test";
NSLog(@"test = %p",test);
NSLog(@"&test = %p",&test);
//不可變字符串copy後的內存地址和指針地址
NSString *test1 = test.copy;
NSLog(@"test1 = %p",test1);
NSLog(@"&test1 = %p",&test1);
//不可變字符串mutableCopy後的內存地址和指針地址
id test2 = test.mutableCopy;
NSLog(@"[test2 class] = %@",[test2 class]);
NSLog(@"test2 = %p",test2);
NSLog(@"&test2 = %p", &test2);
輸出結果:
test = 0x109869290
&test = 0x7ffee65ff738
test1 = 0x109869290
&test1 = 0x7ffee65ff740
[test2 class] = __NSCFString
test2 = 0x600003f34840
&test2 = 0x7ffee65ff748
從輸出結果能夠看出:
(1)非容器不可變對象調用copy方法, 其實只是把當前對象的指針指向了原對象的地址;
(2)調用mutableCopy方法, 則是新分配了一塊內存區域, 並把新對象的指針指向了這塊區域;
複製代碼
//非容器可變字符串的內存地址和指針地址
NSMutableString *mTest1 = [[NSMutableString alloc] initWithString:@"mTest1"];
NSLog(@"mTest1 = %p",mTest1);
NSLog(@"&mTest1 = %p",&mTest1);
//非容器可變字符串copy後的內存地址和指針地址
id mTest1Copy = mTest1.copy;
NSLog(@"[mTest1Copy class] = %@",[mTest1Copy class]);
NSLog(@"mTest1Copy = %p",mTest1Copy);
NSLog(@"&mTest1Copy = %p", &mTest1Copy);
//非容器可變字符串mutableCopy後的內存地址和指針地址
id mTest1MutableCopy = mTest1.mutableCopy;
NSLog(@"[mTest1MutableCopy class] = %@",[mTest1MutableCopy class]);
NSLog(@"mTest1MutableCopy = %p",mTest1MutableCopy );
NSLog(@"&mTest1MutableCopy = %p",&mTest1MutableCopy);
輸出結果:
mTest1 = 0x600002d84e40
&mTest1 = 0x7ffee9ee0750
[mTest1Copy class] = NSTaggedPointerString
mTest1Copy = 0xdcf5175d7b253fbc
&mTest1Copy = 0x7ffee9ee0758
[mTest1MutableCopy class] = __NSCFString
mTest1MutableCopy = 0x600002d800f0
&mTest1MutableCopy = 0x7ffee9ee0760
從輸出結果能夠看出:
非容器可變對象調用copy和mutableCopy方法後都會從新分配一塊內存區域.
複製代碼
//不可變數組的內存地址和指針地址
NSArray *testArray = @[@"1", @"2", @"3"];
NSLog(@"testArray = %p",testArray);
NSLog(@"&testArray = %p", &testArray);
//不可變數組copy後的內存地址和指針地址
NSArray *testArrayCopy = testArray.copy;
NSLog(@"[testArrayCopy class] = %@",[testArrayCopy class]);
NSLog(@"testArrayCopy = %p",testArrayCopy);
NSLog(@"&testArrayCopy = %p",&testArrayCopy);
//不可變數組mutableCopy後的內存地址和指針地址
id testArrayMutableCopy = testArray.mutableCopy;
NSLog(@"[testArrayMutableCopy class] = %@",[testArrayMutableCopy class]);
NSLog(@"testArrayMutableCopy = %p",testArrayMutableCopy);
NSLog(@"&testArrayMutableCopy = %p",&testArrayMutableCopy);
輸出結果:
testArray = 0x600001780a50
&testArray = 0x7ffeee00e730
[testArrayCopy class] = __NSArrayI
testArrayCopy = 0x600001780a50
&testArrayCopy = 0x7ffeee00e738
[testArrayMutableCopy class] = __NSArrayM
testArrayMutableCopy = 0x6000017b0270
&testArrayMutableCopy = 0x7ffeee00e718
從輸出結果能夠看出:
(1)不可變容器調用copy方法, 其實只是把當前對象的指針指向了原對象的地址;
(2)調用mutableCopy方法, 則是新分配了一塊內存區域, 並把新對象指向了這塊區域;
複製代碼
//可變數組的內存地址和指針地址
NSMutableArray *testMArray = [NSMutableArray arrayWithArray:@[@"1", @"2", @"3"]];
NSLog(@"testMArray = %p", testMArray);
NSLog(@"&testMArray = %p", &testMArray);
//可變數組copy後的內存地址和指針地址
id testMArrayCopy = testMArray.copy;
NSLog(@"[testMArrayCopy class] = %@",[testMArrayCopy class]);
NSLog(@"testMArrayCopy = %p",testMArrayCopy);
NSLog(@"&testMArrayCopy = %p", &testMArrayCopy);
//可變數組mutableCopy後的內存地址和指針地址
id testMArrayMutableCopy = testMArray.mutableCopy;
NSLog(@"[testMArrayMutableCopy class] = %@", [testMArrayMutableCopy class]);
NSLog(@"testMArrayMutableCopy = %p",testMArrayMutableCopy);
NSLog(@"&testMArrayMutableCopy = %p", &testMArrayMutableCopy);
輸出結果:
testMArray = 0x600003950570
&testMArray = 0x7ffee41d9728
[testMArrayCopy class] = __NSArrayI
testMArrayCopy = 0x6000039547b0
&testMArrayCopy = 0x7ffee41d9730
[testMArrayMutableCopy class] = __NSArrayM
testMArrayMutableCopy = 0x60000395a160
&testMArrayMutableCopy = 0x7ffee41d9738
從輸出結果能夠看出:
可變容器調用copy和mutableCopy方法後都會從新分配一塊內存;
複製代碼
總結:
(1)非容器對象(NSString)
不可變對象調用copy方法其實只是把當前對象的指針指向了原對象的地址; 而調用mutableCopy方法則是從新分配了一塊內存區域並把新對象的指針指向了這塊區域;數組
可變對象調用copy和mutableCopy方法都會從新分配一塊內存區域.bash
(2)容器類對象(NSArray)
容器對象自己和非容器對象是一樣的效果.ui
參考: www.cnblogs.com/yxl-151217/…spa