1. Copying variable strings
nsmutablestring * City = [nsmutablestring stringwithstring:@ "Beijing"]; Copy a mutable copy nsmutablestring * citycopy = [city mutablecopy]; Modify the copy [Citycopy replacecharactersinrange:nsmakerange (0, 2) withstring:@ "Lincang"]; Output see the original string does not change the current change NSLog (@ "The original city is%@, now the citycopy is%@", city,citycopy);
2. Copying mutable copies of immutable strings
NSString * STR [email protected] "test string"; nsmutablestring * strcopy = [str mutablecopy]; Add String [Strcopy appendstring:@ "and add a string"]; NSLog (@ "Strcopy is%@", strcopy); Returns an immutable copy for a mutable string nsmutablestring * cityCopy2 = [city copy]; Because the city is immutable the following code is wrong [CityCopy2 appendstring:@ "It is wrong"];
Error message
2015-05-05 17:19:37.988 copydemo[424:303] * * * terminating app due to uncaught exception ' nsinvalidargumentexcept Ion ', Reason: ' Attempt to mutate immutable object with appendString: '
First throw Call stack:
Summarizes whether the string returned when copying is not related to the original string or to the type given at the time of copying. Mainly depends on whether the copy method or the Mutablecopy method is called.
3. Object Copy
We need to implement nscopying and nsmutablecopying for custom objects
@interface Person:nsobject@property (Nonatomic,strong) nsmutablestring *name; @property (nonatomic,assign) int age;@ End
#import "Person.h" @interface person () <NSCopying> @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 <Foundation/Foundation.h> #import "Person.h" int main (int argc, const char * argv[]) { @autoreleasepool {person * person = [person new]; Person.name = [nsmutablestring stringwithstring:@ "Xiao Ming"]; Person.age =; Person * personcopy = [person copy];//replica personcopy.name = [nsmutablestring stringwithstring:@ "Xiao Qiang"]; Personcopy.age =23; NSLog (@ "person's name is%@", person.name); NSLog (@ "person's N Age is%d", person.age); NSLog (@ "Personcopy's name is%@", personcopy.name); NSLog (@ "Personcopy's age is%d", personcopy.age);// 2015-05-05 17:38:43.980 copydemo[531:303] person's name is Xiao Ming// 2015-05-05 17:38:43.981 copydemo[531:303] person's n age is 20// 2015-05-05 17:38:43.981 copydemo[531:303] Personcopy's name is Xiao Qiang// 2015-05-05 17:38:43.982 copydemo[531:303] personcopy age is 0} return;
4. Shallow copy
Other invariant modify main function
Person * person = [person new]; Person.name = [nsmutablestring stringwithstring:@ "Xiao Ming"]; Person.age =; Person * personcopy = [person copy];//replica [personcopy.name replacecharactersinrange:nsmakerange (0,2) withstring: @ "changed"]; Personcopy.age =23; NSLog (@ "person's name is%@", person.name); NSLog (@ "person's N Age is%d", person.age); NSLog (@ "Personcopy's name is%@", personcopy.name); NSLog (@ "Personcopy's age is%d", personcopy.age);
The print out is
2015-05-05 17:54:05.347 copydemo[563:303] The name of the person is changed.
2015-05-05 17:54:05.349 copydemo[563:303] person's n age is 20
2015-05-05 17:54:05.350 copydemo[563:303] Personcopy's name is changed.
2015-05-05 17:54:05.350 copydemo[563:303] personcopy age is 23
I was confused at first. The gap is just one line of code. But there's still a difference.
The reason is
Person.name = Self.name; Person.age = Self.age;
Name is a pointer variable that stores only the string. So the name of the modified copy, the original name will also be changed, using Personcopy.name = [nsmutablestring stringwithstring:@ "Xiao Qiang"], this code is actually directly re-assigned value, Points to another string pointer, so the copied object has been modified. The original has not changed. But it does not mean that the copy does not point to a string. It was only later modified.
5. Deep copy
Change the Copywithzone method inside the
Person.name = [Self.name mutablecopy]; Person.age = Self.age;
This enables deep replication.
However, under normal circumstances, deep replication is more difficult. Especially when there are many pointers, fundation are generally shallow copies.
Replication of 6.SETETR methods
Modify the following code
@property (nonatomic,copy) nsmutablestring *name;
Modify Main
Person * person = [person new]; Person.name = [nsmutablestring stringwithstring:@ "Xiao Ming"]; The following line of code will be an error. [Person.name appendstring:@ "will be an error"];
The property of name uses the copy indicator, which is actually equivalent to the Setnmame method.
-(void) setName (NSString *) name{name = [name copy];}
is an immutable copy of the parameter
Summary of iOS development replication (deep copy shallow copy difference)