問題
當NSString/NSArray/NSDictionary類型時,屬性標識符你使用copy or strong?
對象接收到copy消息會觸發(fā)怎樣的動作
對象接收到copy消息互躬,如果對象的類型已經(jīng)遵守了NSCopying協(xié)議那么會觸發(fā)- (id)copyWithZone:(nullable NSZone *)zone
;如果未遵守該協(xié)議那么-[User copyWithZone:]: unrecognized selector sent to instance 0x100203ed0
NSCopying協(xié)議的內(nèi)容
@protocol NSCopying
//Returns a new instance that’s a copy of the receiver.
- (id)copyWithZone:(nullable NSZone *)zone;
@end
收到copy消息的對象都會創(chuàng)建一個新的實例對象嗎根悼?
- (void)testMethod {
#pragma mark - NSString
NSString* k_name = [NSString stringWithFormat:@"%@",@"HZiOS"];
NSLog(@"(1)%@---%p",k_name,k_name);
NSString* another_name = [k_name copy];
NSLog(@"(2)%@---%p",another_name,another_name);
#pragma mark - NSMutableString
NSMutableString* kmutable_name = [NSMutableString stringWithFormat:@"%@",@"HZiOSmutable"];
NSLog(@"(3)%@---%p",kmutable_name,kmutable_name);
NSString* other_name = [kmutable_name copy];
NSLog(@"(4)%@---%p",other_name,other_name);
/*
(1)HZiOS---0x534f695a4855
(2)HZiOS---0x534f695a4855
(3)HZiOSmutable---0x100207120
(4)HZiOSmutable---0x100700110
*/
}
NSString不可變字符串
上述代碼中NSString類型的變量在收到copy消息的時候并未返回一個全新的實例對象节预,可以看到兩個變量的內(nèi)存地址是一樣的惭适;由此可以猜想在NSString類型的變量收到copy消息時在- (id)copyWithZone:(nullable NSZone *)zone;
方法中做了邏輯判斷科阎,[k_name isKindOfClass:[NSString class]]
此條件成立則直接返回當前實例對象辆苔;
NSMutableString可變字符串
上述代碼中NSMutableString的變量kmutable_name收到copy消息返回了一個不可變NSString的變量other_name算灸;兩個變量的內(nèi)存地址不一致;這就說明重新創(chuàng)建一個全新的實例變量驻啤;
為什么NSString和NSMutableString接收到copy消息操作會不一樣呢乎婿?
- NSString類型接收到copy消息如果返回一個新實例,那么內(nèi)存中就存在兩塊內(nèi)存空間存儲相同值的不可變字符串街佑,這樣做的話會浪費本來就很稀缺的內(nèi)存空間谢翎,所以具體實現(xiàn)上apple的工程師應該做一些優(yōu)化;
- NSMutableString類型接收到消息返回一個新實例沐旨,原因在于它返回的是一個不可變對象森逮,必須是一個新的實例,如何返回內(nèi)存地址一致那么后期的操作上會引發(fā)異常磁携;
NSArray類型成員變量的修飾符屬性你使用copy or strong褒侧?
答案是使用copy;為什么使用copy這個就是為了完全避免被子類類型的變量賦值后導致的程序異常谊迄;可以試驗將下列代碼中的屬性標識符copy換成strong闷供;看一下運行的結(jié)果;
#import "HZPersonCopy.h"
@interface HZPersonCopy ()
@property(nonatomic,copy)NSArray* array;
@end
@implementation HZPersonCopy
-(void)test{
self.array = @[@"1",@"2"];
NSMutableArray* mutableArray =[@[@"1",@"2",@"3"] mutableCopy];
self.array = mutableArray;
NSLog(@"--->:%@",self.array[2]);
[mutableArray removeLastObject];
NSLog(@"--->:%@",self.array[2]);
}
總結(jié):
當屬性類型為NSString统诺,并且賦值的類型也是NSString(對于不可變類型的集合同樣適用)那么使用copy和strong沒有區(qū)別歪脏,但還是建議使用copy;
-(void)testCopyStr{
#pragma mark - copy屬性
NSString* k_name = [NSString stringWithFormat:@"%@",@"HZiOS"];
NSLog(@"%@---%p",k_name,k_name);
self.name = k_name;
NSLog(@"%@---%p",self.name,self.name);
k_name = [NSString stringWithFormat:@"%@",@"anotherStr"];
NSLog(@"%@---%p",k_name,k_name);
NSLog(@"%@---%p",self.name,self.name);
#pragma mark - test copy and mutableCopy
self.name = [k_name copy];
NSLog(@"%@---%p",self.name,self.name);
self.name = [k_name mutableCopy];
NSLog(@"%@---%p",self.name,self.name);
NSLog(@"----------------------------------");
/*
HZiOS---0x534f695a4855
HZiOS---0x534f695a4855
anotherStr---0x10b192a02d085a5
HZiOS---0x534f695a4855
anotherStr---0x10b192a02d085a5
anotherStr---0x10b192a02d085a5
*/
}
-(void)testStrongStr{
#pragma mark - strong屬性
NSString* k_name = [NSString stringWithFormat:@"%@",@"HZiOS1"];
NSLog(@"%@---%p",k_name,k_name);
self.anotherName = k_name;
NSLog(@"%@---%p",self.anotherName,self.anotherName);
k_name = [NSString stringWithFormat:@"%@",@"anotherStr1"];
NSLog(@"%@---%p",k_name,k_name);
NSLog(@"%@---%p",self.anotherName,self.anotherName);
#pragma mark - test copy and mutableCopy
self.anotherName = [k_name copy];
NSLog(@"%@---%p",self.anotherName,self.anotherName);
self.anotherName = [k_name mutableCopy];
NSLog(@"%@---%p",self.anotherName,self.anotherName);
/*
HZiOS1---0x1002036a0
HZiOS1---0x1002036a0
HZiOS1addStr---0x1002036a0
HZiOS1addStr---0x1002036a0
HZiOS1addStr---0x1001029b0
HZiOS1addStr---0x100700150
*/
}
唯一需要注意的是屬性類型為NSString時粮呢,接收到[xxx mutableCopy]的返回值婿失,使用copy和strong會略有不同钞艇;這個可以看代碼中的內(nèi)存地址;
copy屬性特征:接收到[xxx mutableCopy]的返回值豪硅,實際又接收了copy消息哩照,這樣在內(nèi)存中有存在兩塊內(nèi)存空間存儲相同的不可變字符,so優(yōu)化合并懒浮,所以打印出來的內(nèi)存地址是一樣的飘弧;