Objective
Three ways to copy OC objects
The object copy of OC has the following three ways, many times we confuse deep copy with full copy, other they are different, specifically as follows
Shallow copy: in a shallow copy operation, a pointer copy is shallow for each layer of the object being copied.
deep copy: in a deep copy operation, at least one layer is deep copy for the object being replicated. \ one-level-deep
Full copy (real-deep copy): in a full copy operation, object replication is replicated for each layer of the object being replicated.
Two figures to avoid the
Understanding deep Copy (Mutablecopy)
Shallow copy is very simple, do not demonstrate, look at the above diagram to understand, just a simple pointer copy, so change the original object or copy of the object, will affect another object.
From the above we can see mutableCopy
that for any object is content replication, that is, a deep copy.
Code on:
Nsmutablearray * Dataarray1=[nsmutablearray arraywithobjects: [NSMutabl Estring stringwithstring:@ "1"], [nsmutablestring stringwithstring:@ "2"], [nsmutablestring
stringwithstring:@ "3"], [nsmutablestring stringwithstring:@ "4"], nil];
Nsmutablearray * Dataarray2=[nsmutablearray arraywithobjects: [nsmutablestring stringwithstring:@ "one"],
[Nsmutablestring stringwithstring:@ "Two"], [nsmutablestring stringwithstring:@ "three"],
[Nsmutablestring stringwithstring:@ "Four"], DataArray1, nil
];
Nsmutablearray * DATAARRAY3;
Nsmutablestring * MSTR;
Dataarray3=[dataarray2 Mutablecopy];
MSTR = dataarray2[0];
[Mstr appendstring:@ "--one"];
NSLog (@ "dataarray3:%@", dataArray3);
NSLog (@ "dataarray2:%@", dataArray2);
The output is as follows:
2016-07-31 17:40:30.702 test1[2113:169774] dataArray3: (
"One--one",
two,
three,
four,
(
1 ,
2,
3,
4
)
)
2016-07-31 17:40:30.703 test1[2113:169774] dataArray2: (
"One--one" ,
Two,
three,
four,
(
1,
2,
3,
4
)
)
Look at the output above, we found that we change the original array dataArray2
, it will also affect the deep copy dataArray3
, is not a good copy of the content, why this?
Here we say the difference between deep copy and full copy .
We know that deep copy, is to clone the contents of the original object directly to the new object, but here is a hole that he will only copy a layer of objects, but not duplicate the second layer or even deeper objects.
The code is a copy of the contents of the dataArray3=[dataArray2 mutableCopy];
array dataArray2
itself, but the string object inside is not a copy of the content, but rather a shallow copy, dataArray2
and dataArray3
the inside object is shared. That's why the above is happening.
Single-Layer deep copy
So how to solve the above problems?
You can use the following code
Dataarray3=[[nsmutablearray Alloc]initwitharray:dataarray2 Copyitems:yes];
The output is as follows:
2016-07-31 17:45:48.472 test1[2151:173221] DataArray3: (one
,
two,
three,
four,
(
1,
2 ,
3,
4
)
2016-07-31 17:45:48.472 test1[2151:173221] dataArray2: (
"One--one",
Two,
three,
four,
(
1,
2,
3,
4
)
)
Can be seen dataArray3
and not changed, but don't be happy too early, we have to change.
The code is as follows:
Nsmutablearray * Dataarray1=[nsmutablearray arraywithobjects: [nsmutablestring stringwithstring:@ "1"],
[Nsmutablestring stringwithstring:@ "2"], [nsmutablestring stringwithstring:@ "3"],
[Nsmutablestring stringwithstring:@ "4"], nil];
Nsmutablearray * Dataarray2=[nsmutablearray arraywithobjects: [nsmutablestring stringwithstring:@ "one"],
[Nsmutablestring stringwithstring:@ "Two"], [nsmutablestring stringwithstring:@ "three"], [Nsmutablestring stringwithstring:@ "Four"], DataArray1, nil]
;
Nsmutablearray * DATAARRAY3;
Nsmutablestring * MSTR;
Dataarray3=[[nsmutablearray Alloc]initwitharray:dataarray2 Copyitems:yes];
Nsmutablearray *marr = (Nsmutablearray *) dataarray2[4];
MSTR = marr[0];
[Mstr appendstring:@ "--one"];
NSLog (@ "dataarray3:%@", dataArray3); NSLog (@ "dataarray2:%@ ", dataArray2);
The output is as follows:
2016-07-31 17:47:19.421 test1[2174:174714] DataArray3: (one
,
two,
three,
four,
(
"1--one") ,
2,
3,
4
)
)
2016-07-31 17:47:19.421 test1[2174:174714] DataArray2: (One
,
Two,
three,
four,
(
"1--one",
2,
3,
4
)
)
You can see that the deep copy fails again, because dataArray3=[[NSMutableArray alloc]initWithArray:dataArray2 copyItems:YES];
only one layer of deep replication can be done for the second or more layers.
Don't worry, we still have a big trick.
Full replication
To replicate a Multi-tiered collection object, we need to make a full copy, where we can use the archive and the Access file.
The implementation code is as follows:
DataArray3 = [Nskeyedunarchiver unarchiveobjectwithdata:[nskeyedarchiver archiveddatawithrootobject:dataarray2]];
At this point the output is as follows:
2016-07-31 17:49:55.561 test1[2202:177163] DataArray3: (one
,
two,
three,
four,
(
1,
2 ,
3,
4
)
)
2016-07-31 17:49:55.562 test1[2202:177163] DataArray2: (one
,
two,
Three,
four,
(
"1--one",
2,
3,
4
)
)
Can be seen dataArray3
without being dataArray2
affected by the modification.
Class replication
After we've finished copying the objects, let's take a look at how to implement the replication of the class, because it's easier to put code directly
Defining class Replication
#import <Foundation/Foundation.h>
@interface person:nsobject<nscopying>
@property (Strong, nonatomic) NSString *age;
@property (strong,nonatomic) nsstring *name;
@end
#import "Person.h"
@implementation person
-(ID) Copywithzone: (Nszone *) zone
{person
*person = [ Person Allocwithzone:zone] init];
Person.age = Self.age;
Person.name = Self.name;
return person;
}
@end
Call
Person *person = [[Person alloc]init];
Person.age = @ "DSDSD";
Person.name = @ "DSDSDDDWW";
Person *copyperson = [person copy];
NSLog (@ "%@-----%@", copyperson.age, Copyperson.name);
You can see copyPerson
two properties and the persona
like.
Copy keyword in @property
When setting the properties of a nsstring type, we'd better set it to the copy type so that when someone uses the property we define, whatever changes the property's assignment to, it doesn't affect the value we assign to the property.
Now let's take a look at
As shown in the figure above, string2
the property is a copy
type and can be seen to be unmodified.
Because at this time string2
and copystring
the memory address is not the same, modify one, will not affect the other one.
As shown in the figure above, if string2
the property is a strong
type, it can be modified, as shown in the following illustration:
Because at this time string2
and copystring
the memory address is the same, modify one, the two are also modified
Nsmutablestring crash of the Copy keyword
Reason:
copy
The method of the keyword string
setter
is actually to assign the parameter copy to the variable _string
, then the variable _string
is declared as NSMutableString
, but then the copy
variable _string
NSString
is changed to an immutable type. So there will be a method of error, prompt to the immutable NSString
use NSMutableString
of the method appendString
.
Summarize
The above is the details of the depth of iOS copy, I hope this article in the development of iOS in the process can help.