Deep Copy and shallow Copy (mutableCopy and Copy)

Source: Internet
Author: User

Reference: http://www.cnblogs.com/ydhliphonedev/archive/2012/04/27/2473927.html

Ios provides the copy and mutablecopy methods. As the name suggests, copy Copies an imutable object, while mutablecopy copies a mutable object. The following are examples.

1. Non-container objects of the system: this refers to objects such as NSString and NSNumber.

NSString *string = @"origion";NSString *stringCopy = [string copy];NSMutableString *stringMCopy = [string mutableCopy];[stringMCopy appendString:@"!!"];

Check the memory. We can find that string and stringCopy point to the same memory area (also called apple's weak reference). In this case, the reference count of stringCopy is the same as that of string. StringMCopy is what we call true replication. The system allocates new memory for it, but the Pointer Points to the same string as the string.

Let's look at the following example:

NSMutableString *string = [NSMutableString stringWithString: @"origion"];NSString *stringCopy = [string copy]; NSMutableString *mStringCopy = [string copy];NSMutableString *stringMCopy = [string mutableCopy];[mStringCopy appendString:@"mm"];//error[string appendString:@" origion!"];[stringMCopy appendString:@"!!"];

The memory allocated by the above four NSString objects is different. However, mStringCopy is actually an imutable object. Therefore, an error is returned.
For non-container objects in the system, we can think that for an immutable object, copy is Pointer copy (Shortest copy) and mutableCopy is object copy (deep copy ). If you copy a mutable object, it is a deep copy, but the object returned by copy is immutable.

2. system container objects: NSArray and NSDictionary. For the container class itself, the conclusions discussed above are also applicable. We need to discuss the changes of objects in the container after replication.

// Copy returns an immutable object. mutablecopy returns the variable object NSArray * array1 = [NSArray arrayWithObjects: @ "a", @ "B", @ "c", nil]; NSArray * arrayCopy1 = [array1 copy]; // arrayCopy1 is the same NSArray object as array (pointing to the same object ), the elements in the array also point to the same NSLog (@ "array1 retain count: % d", [array1 retainCount]); NSLog (@ "array1 retain count: % d ", [arrayCopy1 retainCount]); NSMutableArray * mArrayCopy1 = [array1 mutableCopy]; // mArrayCopy1 is a mutable copy of array1, pointing to different objects than array1, Is where the element and the element in array1 point to the same object. MArrayCopy1 can also modify its own object [mArrayCopy1 addObject: @ "de"]; [mArrayCopy1 removeObjectAtIndex: 0];

Array1 and arrayCopy1 are pointer replication, while mArrayCopy1 is object replication. mArrayCopy1 can also change the elements in the period: delete or add. But note that all elements in the container are pointer copies.
Next we will use another example to test it.

NSArray * mArray1 = [NSArray arrayWithObjects: [NSMutableString stringWithString: @ "a"], @ "B", @ "c", nil]; NSArray * mArrayCopy2 = [mArray1 copy]; NSLog (@ "mArray1 retain count: % d", [mArray1 retainCount]); NSMutableArray * mArrayMCopy1 = [mArray1 mutableCopy]; NSLog (@ "mArray1 retain count: % d ", [mArray1 retainCount]); // mArrayCopy2, mArrayMCopy1 and mArray1 point to different objects, however, all the elements are the same object -- the same pointer // test NSMutableString * testString = [mArray1 objectAtIndex: 0]; // testString = @ "1a1 "; // This will change the pointer to testString. In fact, the @ "1a1" temporary object is assigned to testString [testString appendString: @ "tail"]; // The first element of the preceding three arrays is changed.

It can be seen that the element object of a container is always pointer replication. If you want to copy element objects as objects, You need to perform deep copy.

 

 

 

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.