IOS development and replication Summary (deep copy and shallow copy differences)

Source: Internet
Author: User

IOS development and replication Summary (deep copy and shallow copy differences)
1. Copy a variable string

NSMutableString * city = [NSMutableString stringWithString: @ "Beijing"]; // copy the variable copy NSMutableString * cityCopy = [city mutableCopy]; // modify the copy [cityCopy sequence: NSMakeRange (0, 2) withString: @ "Lincang"]; // The output shows that the original string does not change the current NSLog (@ "the original city is % @, and the current cityCopy is % @", city, cityCopy );

2. Copy a variable copy of an immutable string
NSString * str = @ "test string"; NSMutableString * strCopy = [str mutableCopy]; // Add a string [strCopy appendString: @ "add another string"]; NSLog (@ "strCopy is % @", strCopy); // returns an immutable copy for a variable string NSMutableString * cityCopy2 = [city copy]; // The following code is incorrect because the city is unchangeable [cityCopy2 appendString: @ "this is wrong"];
Error Message
// 17:19:37. 988 copyDemo [424: 303] *** Terminating app due to uncaught exception 'nsinvalidargumentexception ', reason: 'attempt to mutate immutable object with appendString :'
// *** First throw call stack:
Whether the returned string is variable during the summary copy is irrelevant to the original string or the type given during the copy. It mainly depends on whether the copy method is called or the mutableCopy method.

3. Object copy

For custom objects, we need to implement NSCopying and NSMutableCopying.
@interface Person : NSObject@property(nonatomic,strong)NSMutableString *name;@property(nonatomic,assign)int age;@end
#import "Person.h"@interface Person()
 
  @end@implementation Person- (id)copyWithZone:(NSZone *)zone{    Person * person = [[[self class] allocWithZone:zone] init];    person.name = self.name;    person.age = self.age;    return person;}@end
 
# Import
 
  
# Import "Person. h "int main (int argc, const char * argv []) {@ autoreleasepool {Person * person = [Person new]; person. name = [NSMutableString stringWithString: @ "Xiao Ming"]; person. age = 20; Person * personCopy = [person copy]; // copy a copy of personCopy. name = [NSMutableString stringWithString: @ "Xiaoqiang"]; personCopy. age = 23; NSLog (@ "person name is % @", person. name); NSLog (@ "the n age of person is % d", person. age); NSLog (@ "personCopy name is % @", personCopy. name); NSLog (@ "the age of personCopy is % d", personCopy. age); // 17:38:43. 980 copyDemo [531: 303] The Name of person is James // 17:38:43. 981 copyDemo [531: 303] The n age of person is 20 // 17:38:43. 981 copyDemo [531: 303] the name of personCopy is Xiaoqiang // 17:38:43. 982 copyDemo [531: 303] The Age Of personCopy is 23} return 0 ;}
 

4. Shallow copy

Other functions without changing the main function
Person * person = [Person new]; person. name = [NSMutableString stringWithString: @ "Xiao Ming"]; person. age = 20; Person * personCopy = [person copy]; // copy a copy [personCopy. name replaceCharactersInRange: NSMakeRange (0, 2) withString: @ "changed"]; personCopy. age = 23; NSLog (@ "person name is % @", person. name); NSLog (@ "the n age of person is % d", person. age); NSLog (@ "personCopy name is % @", personCopy. name); NSLog (@ "the age of personCopy is % d", personCopy. age );
The output is
17:54:05. 347 copyDemo [563: 303] is the name of person changed?
17:54:05. 349 copyDemo [563: 303] The n age of person is 20
17:54:05. 350 copyDemo [563: 303] is the name of personCopy changed?

17:54:05. 350 copyDemo [563: 303] age of personCopy is 23

At first, I was confused. There is only one code gap. However, there is still a difference.
The reason is that
    person.name = self.name;    person.age = self.age;
Name is a pointer variable and only stores strings. Therefore, if the copy name is modified, the original name will also change. Use personCopy. name = [NSMutableString stringWithString: @ ""]; this code is actually re-assigned directly, pointing to another string pointer, so the Copied object is modified. The original one has not changed. However, it does not mean that the object is not copied to a string. It was modified later.

5. Deep copy

Modify the copyWithZone Method

    person.name = [self.name mutableCopy];    person.age = self.age;
In this way, deep replication is realized.
However, deep replication is generally difficult. In particular, when there are many pointers, Fundation is generally light replication.

6. replication of the setetr Method

Modify the following code:
@property(nonatomic,copy)NSMutableString *name;
Modify main
Person * person = [Person new]; person. name = [NSMutableString stringWithString: @ "James"]; // the following line of code reports an error. [Person. name appendString: @ "an error will be reported"];
If the name attribute uses the copy indicator, it is actually equivalent to the setNmame method.
- (void)setName(NSString *)name{name = [name copy];}
Is an immutable copy of parameters

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.