Deep copy and shallow copy in iOS

Source: Internet
Author: User

Many languages have a deep copy of the concept of shallow copy, such as C++,OBJC. Simply put, a shallow copy is two variables pointing to the same chunk of memory, and a deep copy is two variables pointing to different memory areas, but the contents of the two memory areas are the same.

Shallow copy:

Deep copy:

In iOS development, shallow copy and deep copy are more complex, involving both copy and Mutablecopy of collection objects and non-collection objects.

Non-collection objects: such as Nsstring,nsinteger,nsnumber ...

Collection objects: such as Nsarray,nsdictionary, ...

1: Copy and mutablecopy of non-collection objects.

Copy and mutablecopy for non-collection objects, just follow these rules:

(1) Copy and Mutablecopy methods for variable objects are deep copy

(2) The copy method of the immutable object is shallow copy, Mutablecopy method is deep copy

(3) The object returned by the copy method is an immutable object

The following code validates:

Copy and Mutablecopy Methods for mutable objects:

voidtestmutable () {//replication of Mutable objects, copy and Mutablecopy are both deep copiesnsmutablestring *str1 = [nsmutablestring stringwithstring:@"Test"]; Nsmutablestring*STR2 =[str1 copy]; //copy returns an immutable object, so the str2 cannot be changed and a crash occurs//[str2 appendstring:@ "test"];Nsmutablestring *STR3 =[str1 mutablecopy]; [Str3 appendString:@"Test"]; NSLog (@"%@ %@ %@", STR1,STR2,STR3); NSLog (@"%p%p%p", STR1,STR2,STR3);}

Execution Result:

As you can see, the addresses of the three strings are not the same, stating that both the copy and Mutablecopy methods are deep copies.

Copy and Mutablecopy Methods for immutable objects:

voidtestnomutable () {NSString*STR1 =@"Test"; //Direct copy is a shallow copyNsmutablestring *STR2 =[str1 copy]; //copy returns an immutable object, STR2 cannot be modified, so a crash occurs//[str2 appendstring:@ "test"]; //Mutablecopy is a deep copyNsmutablestring *STR3 =[str1 mutablecopy]; [Str3 appendString:@"Test"]; NSLog (@"%@ %@ %@", STR1,STR2,STR3); NSLog (@"%p%p%p", STR1,STR2,STR3);}

Execution Result:

As you can see: the first two addresses are the same, the third address is different, so the copy method of the immutable object is shallow copy, and the Mutablecopy method is deep copy.

It is also important to note that the object returned by the copy method is immutable, whether it is a mutable object or an immutable object.

2: Copy and Mutablecopy of collection objects

In fact, the rules followed by the collection object and the non-collection object are essentially the same.

Copy and Mutablecopy methods for variable objects

Verification Code:

voidtestsetmutable () {Nsmutablearray*array1 = [Nsmutablearray arraywithobjects:[nsmutablestring stringwithstring:@"a"],@"b",@"C", nil]; //Variable object Copy is a deep copyNsmutablearray *array2 =[array1 copy]; //copy returns an immutable object, Array2 cannot be modified, and therefore crashes//[Array2 addobject:@ "D"]; //the mutablecopy of a mutable object is a deep copyNsmutablearray *array3 =[Array1 mutablecopy]; [Array3 AddObject:@"D"]; NSLog (@"%p%p%p", array1,array2,array3);}

Execution Result:

You can see that the address is not the same. The copy and Mutablecopy methods that describe mutable objects are deep copies.

Copy and Mutablecopy methods for immutable objects

Verification Code:

voidtestsetnomutable () {Nsarray*array1 = @[@"a",@"b",@"C"]; //Copy method of immutable object, shallow copyNsarray *array2 =[array1 copy]; //Mutablecopy method for immutable objects, deep copyNsarray *array3 =[Array1 mutablecopy]; NSLog (@"%p%p%p", array1,array2,array3);}

Execution Result:

As you can see, the first two addresses are the same, and the third one is different. The copy method that describes an immutable object is a shallow copy, and the Mutablecopy method is a deep copy.

A difference between a collection object and a non-collection object:

The deep copy of the collection object mentioned above is not a deep copy in strict sense, but a single layer deep copy.

Single-Layer deep copy: For a Collection object, a deep copy is only a deep copy of the first layer object, and the inner object is still shallow copied. For example

Nsmutablearray *array1 = [Nsmutablearray arraywithobjects:[nsmutablestring stringwithstring:@ "a "],@"b",@"C", nil];

Call the Copy method Nsarray *array2 = [array1 copy], there is a memory allocated, Array2 points to the memory, but the elements inside the array, pointing to the elements inside the array 1, that is, the inner element is shallow copy.

Verification Code:

voidtestsetmutable () {Nsmutablearray*array1 = [Nsmutablearray arraywithobjects:[nsmutablestring stringwithstring:@"a"],@"b",@"C", nil]; //Variable object Copy is a deep copyNsmutablearray *array2 =[array1 copy]; //copy returns an immutable object, Array2 cannot be modified, and therefore crashes//[Array2 addobject:@ "D"]; //the mutablecopy of a mutable object is a deep copyNsmutablearray *array3 =[Array1 mutablecopy]; [Array3 AddObject:@"D"]; NSLog (@"%p%p%p", ARRAY1,ARRAY2,ARRAY3); NSLog (@"%p%p%p", array1[0],array2[0],array3[0]);}

Execution Result:

As you can see, the address of the first element of the three array is the same, which means that the inner element is shallow copied.

3: Full replication of collection objects

The full replication of the collection object is that the elements of each layer in the collection are deep copies.

Method One:

Use initwith***: Copyitems:yes method.

Code:

voidtestfullcopy () {Nsmutablearray*array1 = [Nsmutablearray arraywithobjects:[nsmutablestring stringwithstring:@"a"],@"b",@"C", nil]; Nsarray*array2 =[[Nsarray alloc] Initwitharray:array1 Copyitems:yes]; NSLog (@"%p%p", Array1,array2); NSLog (@"%p%p", array1[0],array2[0]);}

Execution Result:

You can see that the address of the array element is different.

Method Two:

Archive the collection before you unpack the file. The code is as follows:

voidtestFullCopy2 () {Nsmutablearray*array1 = [Nsmutablearray arraywithobjects:[nsmutablestring stringwithstring:@"a"],@"b",@"C", nil]; Nsarray*array2 =[nskeyedunarchiver unarchivetoplevelobjectwithdata:[nskeyedarchiver archiveddatawithrootobject:array1] Error:    NIL]; NSLog (@"%p%p", Array1,array2); NSLog (@"%p%p", array1[0],array2[0]);}

Execution Result:

You can see that the address of the array element is different.

Deep copy and shallow copy in iOS

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.