Introduction to deep copy, shallow copy, custom object copy of IOS

Source: Internet
Author: User

The purpose of copy syntax: When changing a copy, it does not affect the source object;

Deep copy: A copy of the content that produces a new object. The new object counter is set to 1, and the source object counter is unchanged.

Shallow copy: Pointer copy, does not produce new objects. Source Object Counter +1.

The copy has the following two methods to implement the copy:

-(ID) copy;  

object to implement copy, must implement <NSCopying> protocol
arrays, dictionaries, and strings have already implemented the <NSCopying> protocol, with the following string as an example:

1. Non-variable string call copy implementation (shallow copy)

NSString *string = [[NSString alloc] initwithformat:@ "ABCDE"];  Copy produces an immutable copy, because the source object itself is immutable, so for the sake of performance, copy will directly return the source object itself//  source Object counter will be +1  //In a shallow copy case, copy is actually equivalent to retain  NSString *STR = [string copy];  

2. Immutable string calls Mutablecopy implementation copy, (deep copy)

NSString *string = [[NSString alloc] initwithformat:@ "ABCD"];  A new object is generated with a counter of 1. The counter for the source object does not change.  nsmutablestring *str = [string mutablecopy];//There are two objects at this time//str:1 and//string:1   //STR and string not the same object   //NSLog (@ "%i", str = = string);//0   [str appendstring:@ "ABCD"];//modify STR does not affect string   NSLog (@ "string:%@", string);   NSLog (@ "str:%@", str);  

3. Variable string call copy implementation copy (deep copy)

nsmutablestring *string = [nsmutablestring stringwithformat:@ "Age is%i", ten];   A new object is generated and the str counter is 1   nsstring *str = [string copy];  

4. mutablecopy(deep copy) of variable strings

nsmutablestring *string = [nsmutablestring stringwithformat:@ "Age is%i", ten];  A new object is generated, and the STR counter is 1  nsmutablestring *str = [string mutablecopy];  [Str appendstring:@ "1234"];//modify the new object without affecting the original object  NSLog (@ "str:%@", str);  NSLog (@ "string:%@", string);  

5. Copy the custom object, with the student object as an example

A.student to implement copy, the <NSCopying> protocol must be implemented

B. Ways to implement the <NSCopying> Agreement:

-(ID) Copywithzone: (nszone *) Zone

Student.h file

@interface student:nsobject <NSCopying>    //Copy represents the Set method will release the old object, copy new object  //Modify the outside variables, and does not affect the internal member variable  //Recommendation: NSString General use copy strategy, other objects generally with retain  @property (nonatomic, copy) NSString *name;    + (ID) studentwithname: (NSString *) name;    @end  

STUDENT.M file

#import "Student.h"    @implementation Student    + (ID) studentwithname: (NSString *) name {      //Here it is best to write [self class]      Student *stu = [[[Self class] alloc] init] autorelease];      Stu.name = name;            return stu;  }    -(void) dealloc {      [_name release];            [Super Dealloc];  }    #pragma mark Description method cannot print self internally, otherwise it will cause a dead loop  -(NSString *) Description {      return [NSString stringWithFormat : @ "[name=%@]", _name];  }    #pragma mark Copying Protocol method  //The replica object created here does not require a release  -(ID) Copywithzone: (Nszone *) zone {      Student *copy = [[[Self Class] allocwithzone:zone] init];            Copy the name to the Copy object      copy.name = self.name;            return copy;  }    @end  

Copy Student

Student *STU1 = [Student studentwithname:@ "STU1"];    Student *STU2 = [stu1 copy];  Stu2.name = @ "STU2";    NSLog (@ "stu1:%@", STU1);  NSLog (@ "stu2:%@", STU2);    [STU2 release];  

Summary:

Recommendation: NSString general use copy strategy, other objects generally use retain;

Only one case is a shallow copy: When the immutable object calls the Copy method, the other case is a deep copy;

Introduction to deep copy, shallow copy, custom object copy of 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.