Deep. Shallow copy
- Copy mutablecopy NSString
NSString *string = @"汉斯哈哈哈";// 没有产生新对象NSString *copyString = [stringcopy];// 产生新对象NSMutableString *mutableCopyString = [string mutableCopy];NSLog(@"string = %p copyString = %p mutableCopyString = %p"string, copyString, mutableCopyString);
- Copy mutablecopy nsmutablestring
NSMutableString *string = [NSMutableString stringWithString:@"汉斯哈哈哈"];// 产生新对象NSString *copyString = [string copy];// 产生新对象NSMutableString *mutableCopyString = [string mutableCopy];NSLog(@"string = %p copyString = %p mutableCopyString = %p", string, copyString, mutableCopyString);
Conclusion:
Note: Other objects Nsarray, Nsmutablearray, nsdictionary, nsmutabledictionary apply
20170.0copy// 这里崩溃
Collapse:
Look at the crash information Hsperson should be implemented first:
- (id)copyWithZone:(NSZone *)zone;
Test:
#import Span class= "Hljs-title" > "HSPerson.h" @ Interface hsperson () <nscopying > @end @ Implementation hsperson -(id ) Copywithzone: (Nszone *) zone{return @ "汉斯哈 haha" ;} @end
HSPerson *p = [[HSPerson alloc] init];p.age20;p.height170.0;HSPerson *copyP = [p copy];NSLog(@"copyP: %@", copyP);
You can see that the Copywithzone re-allocates the new memory space:
-(id ) Copywithzone: (Nszone *) zone{Hsperson *person = [[Hsperson allocwithzone:zone] init]; return person; //some people may alloc below, re-initialize space, but this method has assigned you a zone, you do not need to alloc the memory space again //hsperson *person = [[Hsperson alloc] init]; }
*p20170.0*copyP = [p copy];NSLog(@"%p%p", p, copyP);NSLog(@"age = %d height = %f", copyP.age, copyP.height);
Although a copy of the new object is copied, the Age,height value is not copied, then:
- (id)copyWithZone:(NSZone *)zone{ HSPerson *person = [[HSPersonallocWithZone:zone] init]; self.age; self.height; // 这里self其实就要被copy的那个对象,很显然要自己赋值给新对象,所以这里可以控制copy的属性 return person;}
And then you think, nsmutablecopying? Yes, there is this:
- (id)mutableCopyWithZone:(NSZone *)zone{ HSPerson *person = [[HSPerson allocWithZone:zone] init]; person.ageself.age; person.heightself.height; return person;}
What's the difference between nscopying and nsmutablecopying?
In fact, there is no need to have nsmutablecopying, because there is no variable hsperson, but if the object has other behavior, can borrow nsmutablecopying implementation, hahaha
Copy.strong
Say the depth of the copy, understand Copy.strong is much easier!
#import Span class= "Hljs-title" ><FOUNDATION/FOUNDATION.H> @interface hsperson : nsobject @property (nonatomic , copy) nsstring *name; @end
NSMutableString *string = [NSMutableString stringWithFormat:@"汉斯哈哈哈"string;// 不能改变person.name的值,因为其内部copy新的对象[string appendString:@" hans"]; NSLog(@"name = %@", person.name);
Property copy actually did this for name:
- (void)setName:(NSString *)name{ _name = [namecopy];}
What happens if name is Nsmutablestring?
@property (nonatomicNSMutableString *name;
This will be scolded oh, in fact, internal still:
- (void)setName:(NSMutableString *)name{ _name = [namecopy];}
Copy-out is still immutable characters! If someone uses the Nsmutablestring method, it crashes:
@property (nonatomicstrongNSString *name;
NSMutableString *string = [NSMutableString stringWithFormat:@"汉斯哈哈哈"string;// 可以改变person.name的值,因为其内部没有生成新的对象[string appendString:@" hans"];NSLog(@"name = %@", person.name);
Summary: With copy and strong depending on the requirements, if you do not want to be changed by the external copy, and vice versa with strong
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
About IOS: deep. Shallow copy and Copy.strong