IOS開發-深.淺拷貝與copy.strong

深.淺拷貝

  • 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);

結論:app


注意:其餘對象NSArray、NSMutableArray 、NSDictionary、NSMutableDictionary同樣適用測試


  • copy NSObject
HSPerson *p = [[HSPerson alloc] init]; p.age = 20; p.height = 170.0; HSPerson *copyP = [p copy]; // 這裏崩潰

崩潰:ui


看崩潰信息HSPerson應該先實現:atom

- (id)copyWithZone:(NSZone *)zone;

測試:spa

#import "HSPerson.h" @interface HSPerson()<NSCopying> @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);

能夠看出copyWithZone從新分配新的內存空間,則:3d

- (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);

雖然copy了份新的對象,然而age,height值並未copy,那麼:code

- (id)copyWithZone:(NSZone *)zone
{
    HSPerson *person = [[HSPerson allocWithZone:zone] init]; person.age = self.age; person.height = self.height; // 這裏self其實就要被copy的那個對象,很顯然要本身賦值給新對象,因此這裏能夠控制copy的屬性 return person; }

這時你會想,有NSMutableCopying?沒錯,是有這貨:orm

- (id)mutableCopyWithZone:(NSZone *)zone
{
    HSPerson *person = [[HSPerson allocWithZone:zone] init]; person.age = self.age; person.height = self.height; return person; }

NSCopying、NSMutableCopying有啥區別?
其實感受不必有NSMutableCopying,由於壓根就沒可變的HSPerson,但若是該對象有其餘行爲,能夠借用NSMutableCopying實現,哈哈哈對象

property裏的copy、strong區別

說完深淺拷貝,理解property裏的copy、strong就輕鬆多了!blog

  • copy
#import <Foundation/Foundation.h> @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 實際上就對name幹了這個:

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

假設name爲NSMutableString,會發生什麼事?

@property (nonatomic, copy) NSMutableString *name;

這樣會捱罵哦,實際上內部仍是:

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

copy出來的仍然是不可變字符!若是有人用NSMutableString的方法,就會崩潰:


  • 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);

相關文章
相關標籤/搜索