About IOS: deep. Shallow copy and Copy.strong

Source: Internet
Author: User

Deep. Shallow copy

    • Copy mutablecopy NSString

NSString *string = @ "汉斯哈哈哈" ; // 没有产生新对象 NSString *copyString = [string copy]; // 产生新对象 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

    • Copy NSObject

HSPerson *p = [[HSPerson alloc] init]; p.age = 20; p.height = 170.0; HSPerson *copyP = [p copy];  // 这里崩溃

Collapse:

Look at the crash information Hsperson should be implemented first:

-(ID) Copywithzone: (Nszone *) zone;

Test

#import "HSPerson.h" @interface HSPerson()@end @implementation HSPerson - (id)copyWithZone:(NSZone *)zone { return  @ "汉斯哈哈哈" ; } @end

HSPerson *p = [[HSPerson alloc] init]; p.age = 20; p.height = 170.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; // 有些人可能下面alloc,重新初始化空间,但这方法已给你分配了zone,自己就无需再次alloc内存空间了 // HSPerson *person = [[HSPerson alloc] init]; }

HSPerson *p = [[HSPerson alloc] init]; p.age = 20; p.height = 170.0; HSPerson *copyP = [p copy]; NSLog(@ "p = %p copyP = %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 = [[HSPerson allocWithZone:zone] init]; person.age = self.age; person.height = 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.age = self.age; person.height = self.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!

    • Copy

#import @interface HSPerson : NSObject @property (nonatomic, copy) NSString *name; @end

NSMutableString *string = [NSMutableString stringWithFormat:@ "汉斯哈哈哈" ]; HSPerson *person = [[HSPerson alloc] init]; person.name = string; // 不能改变person.name的值,因为其内部copy新的对象 [string appendString:@ " hans" ]; NSLog(@ "name = %@" , person.name);

Property copy actually did this for name:

- ( void )setName:(NSString *)name { _name = [name copy]; }

What happens if name is Nsmutablestring?

@property (nonatomic, copy) nsmutablestring *name;

This will be scolded oh, in fact, internal still:

- ( void )setName:(NSMutableString *)name { _name = [name copy]; }

Copy-out is still immutable characters! If someone uses the Nsmutablestring method, it crashes:

    • Strong

@property (nonatomic, strong) NSString *name;

NSMutableString *string = [NSMutableString stringWithFormat:@ "汉斯哈哈哈" ]; HSPerson *person = [[HSPerson alloc] init]; person.name = 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

About IOS: deep. Shallow copy and Copy.strong

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.