IOS-deep copy and light copy, ios-mutableCopy

Source: Internet
Author: User

IOS-deep copy and light copy, ios-mutableCopy

Shallow copy: only copies the pointer to the object, instead of copying the reference object itself. For shallow replication, A and A_copy point to the same memory resource. Instead, they only copy A pointer and only one copy of the resource of the object itself (Object reference count + 1 ), if we modify A_copy, we find that the object referenced by A is also modified.

Deep replication is easy to understand. There are two independent objects in the memory.

In Objective-C, not all objects support Copy and MutableCopy. classes that comply with the NSCopying protocol can send Copy messages. classes that comply with the NSMutableCopying protocol can send MutableCopy messages.

(1). copy operations on non-collection objects:

In non-collection class objects:

1. copy the immutable object. It is a pointer copy and a content copy during the mutableCopy operation;

2. copy and mutableCopy of mutable objects are both content copies.

The Code is as follows:

[ImmutableObject copy] // shortest copy

[ImmutableObject mutableCopy] // deep copy

[MutableObject copy] // deep copy

[MutableObject mutableCopy] // deep copy

Code:

NSString * string = @ "origin ";

NSString * stringCopy = [stringcopy]; // The object after the shortest copy is not changeable

NSMutableString * mStringCopy = [stringmutableCopy]; // The object after deep replication is variable.

NSLog (@ "% p-% p", string, stringCopy, mStringCopy );

14:56:44. 068 Copy [18197: 493711] 0x100001080-0x100001080-0x1003003e0

View results: the memory addresses of stringCopy and string are the same, and the memory addresses of mStringCopy and string are different.

NSMutableString * string2 = [NSMutableStringstringWithString: @ "origin2"];

NSString * stringCopy2 = [string2copy]; // The object after deep copy is not changeable.

NSMutableString * mStringCopy2 = [string2mutableCopy]; // The object after deep copy is changeable.

NSLog (@ "% p-% p", string2, stringCopy2, mStringCopy2 );

14:56:44. 068 Copy [18197: 493711] 0x100103920-0x326e696769108f75-0x1001039b0

Result: The memory addresses of mStringCopy2, stringCopy2, and string2 are different.

(2) copy and mutableCopy of collection objects

[ImmutableObject copy] // shortest copy

[ImmutableObject mutableCopy] // deep single-layer Replication

[MutableObject copy] // single-layer Deep copy

[MutableObject mutableCopy] // deep single-layer Replication

Code:

NSArray * array = @ [@ [@ "a", @ "B"], @ [@ "c", @ "d"];

NSArray * copyArray = [arraycopy]; // The copied objects cannot be changed.

NSMutableArray * mCopyArray = [arraymutableCopy]; // The object after a single-layer Deep copy is changeable

NSLog (@ "% p-% p", array, copyArray, mCopyArray );

14:56:44. 069 Copy [18197: 493711] 0x100600450-0x100600450-0x1006038e0

View results: the memory addresses of copyArray and array are the same, and the memory addresses of mCopyArray and array are different.

NSMutableArray * array2 = [NSMutableArrayarrayWithObjects: [NSMutableStringstringWithString: @ "a"], @ "B", @ "c", nil];

NSArray * copyArray2 = [arraycopy]; // The object after a single-layer Deep copy is immutable

NSMutableArray * mCopyArray2 = [arraymutableCopy]; // The object after single-layer Deep copy is changeable

NSLog (@ "% p-% p", array2, copyArray2, mCopyArray2 );

14:56:44. 069 Copy [18197: 493711] 0x1001_fd0-0x100600450-0x100107020

Result: The memory addresses of mCopyArray2, copyArray2, and array2 are different.

(3) custom object copy

. H file

@ InterfaceComplex: NSObject <NSCopying> // use the NSCoping Protocol to implement deep copy.

{

Int_real;

Int_imaginary;

}

-(Void) setReal :( int) real andImg :( int) img;

-(Void) Show;

@ End

. M file

@ ImplementationComplex

-(Void) setReal :( int) real andImg :( int) img

{

_ Real = real;

_ Imaginary = img;

}

-(Void) Show

{

NSLog (@ "% d + % di", _ real, _ imaginary );

}

# Pragma mark-NSCopying

-(Id) copyWithZone :( NSZone *) zone

{

Complex * p = [ComplexallocWithZone: zone]; // apply for a Complex memory

[PsetReal: _ realandImg: _ imaginary]; // copy data

Returnp;

}

@ End

NSLog (@ "------------ shallow copy --------------");

Complex * com1 = [[Complexalloc] init];

[Com1setReal: 12 andImg: 3];

// Shallow copy

Complex * com2 = com1;

[Com1Show];

[Com2Show];

15:23:43. 627 Copy [18424: 509266] 12 + 3i

15:23:43. 627 Copy [18424: 509266] 12 + 3i

[Com1setReal: 3 andImg: 5]; // revalue com1

[Com1Show];

[Com2Show];

// The value of com1 is re-assigned, and com2 changes accordingly.

15:23:43. 627 Copy [18424: 509266] 3 + 5i

15:23:43. 627 Copy [18424: 509266] 3 + 5i

[Com2setReal: 10 andImg: 5]; // com2 revalue

[Com1Show];

[Com2Show];

// Com2 is assigned again, And com1 also changes

15:23:43. 628 Copy [18424: 509266] 10 + 5i

15:23:43. 628 Copy [18424: 509266] 10 + 5i

 

NSLog (@ "------------ deep copy ------------");

Complex * comA = [[Complexalloc] init];

[ComAsetReal: 2 andImg: 3];

Complex * comB = [comAcopy]; // use the copy method for deep copy, but the copyWithZone method in the NSCopying protocol must be implemented.

[ComAShow];

[ComBShow];

15:23:43. 628 Copy [18424: 509266] 2 + 3i

15:23:43. 628 Copy [18424: 509266] 2 + 3i

[ComAsetReal: 3 andImg: 4]; // re-assign comA values

[ComAShow];

[ComBShow];

// ComA changes do not cause comB changes

15:23:43. 628 Copy [18424: 509266] 3 + 4i

15:23:43. 628 Copy [18424: 509266] 2 + 3i

[ComBsetReal: 100 andImg: 2]; // re-assign comB values

[ComAShow];

[ComBShow];

// ComB changes do not cause comA changes

15:23:43. 628 Copy [18424: 509266] 3 + 4i

15:23:43. 628 Copy [18424: 509266] 100 + 2i

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.