origin = pt;
- Copy theptaddress of the object into theorigin. Two variables all point to the same address in memory
- Foundation object: Assigning a variable to another object simply creates another reference (address) to the object.
Part 1.copy and Mutablecopy method: Create a copy of an object
- Copy the object (reference)
dataArray2 = dataArray; / / created another reference to the same array object in memory: a total of an array
- Create a copy of an object
dataArray2 = [dataArray mutalbeCopy]; / / create a copy of the dataArray of the letter and copy all the elements: a total of two arrays
- Note : You can create an immutable copy of a mutable object. You can also create a mutable copy of an immutable object.
Part 2. Shallow copy with deep copy part 2.1 shallow copy
NSMutableArray *dataArray = [NSMutableArray arrayWithObjects:
[NSMutableString stringWithString:@"one"],
[NSMutableString stringWithString:@"two"],
[NSMutableString stringWithString:@"three"],nil];
NSMutableArray *dataArray2;
NSMutableString *mStr;
dataArray2 = [dataArray mutableCopy];
/ / The two arrays (dataArray, dataArray2) elements point to the same string in memory, equivalent to an object assigned to another object, is a shallow copy
mStr = dataArray[0];
/ / Retrieve the first element of the dataArray dataArray[0] returned object and an element in the dataArray point to the same object in memory.
[mstr appendString:@"ONE"];
/ / Change the mstr while changing the first element of the dataArray. DataArray2 has also changed due to shallow copying, because shallow copying simply copies the reference to the object and the content is changed when it is changed.
Part 2.2 deep copy
- Creates a copy of the contents of each object in the array, not just the copies of those object references .
- Change the first element in the dataArray2 without changing the first element of the DataArray:
mStr = [NSMutableString stringWithString: dataArray2[0]];
//Create a new string and store it in the first place of dataArray2.
[mStr appendString @"ONE"];
[dataArray2 replaceObjectAtIndex: 0 withObject: mStr];
/ / Change mStr and add to get an array
Part 3. Implementation < nscopying > protocol
-
To replicate your own classes, you must implement them in accordance with the Protocol.
-
Protocol: Implement Copywithzone: Method to respond to a copy message and return an immutable copy
- Protocol: Implement Mutablecopywithzone: Method, return variable copy
Create a new score in the fraction class
@interface Fraction: NSObject <NSCopying>
-(id) copyWithZone: (NSZone *)zone
{
Fraction *newFact = [[Fraction allocWithZone: zone] init];
[newFact setTo: numerator over: denominator];
Return newFact;
}
Fraction *f1 = [[Fraction alloc]init];
Fraction *f2;
[f1 setTo: 2 over 5];
F2 = [f1 copy]; //Generate a copy of f1, generate a new Fraction, copy f1
[f2 setTo: 1 over: 3];
[f1 print]; // 2/5 copy operation has no effect on f1
[f2 print]; // 1/3
Note :
- If you change the copy statement to F2 = F1, changing the F2 will change the F1.
- If a subclass of this class is produced, the Copywithzone: Method is inherited:
Fraction *newFact = [[Fraction allocWithZone: zone] init];
Should be changed to:
Id newFract = [[self class allocWithZone: zone] init];
Part 4. Copying objects using the Set value method and the Value method
Assignment value:
newCard.name = newName; //Set the name of the object (AddressCard) (name)
Set the value method:
-(void) setName: (NSString *) theName{ // simply assign the parameter to the corresponding instance variable}
If you modify the characters of the newname
To avoid changing the values of other objects, it is safer to copy the objects in the set value method.
Use the copy version of the SetName method:
-(void) setName: (NSString *) theName{ name = [theName copy];}
- The copy attribute of the property:
@property (nonatomicNSString *name;
synthesizetogether with use is equivalent to:
-(void) setName: (NSString *) theName{ ifname) namecopy];}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
[Study note-OBJECTIVE-C] "OBJECTIVE-C Programming 6th Edition" chapter 18th copy objects