copy 和strong
1:修飾mutableArra
@property (nonatomic,strong)NSMutableArray * arrStrong;
@property (nonatomic,copy)NSMutableArray * arrCopy;
NSMutableArray不能用copy修飾,因為在set方法的時候會將array拷貝成不可變的。當后面對其進行操作的時候灰會造成系統(tǒng)崩潰
2:修飾string
@property (nonatomic,strong)NSString * strStrong;
@property (nonatomic,copy)NSString * strCopy;
Person * pers = [[Person alloc] init];
NSMutableString * nn = [NSMutableString stringWithString:@"1234567"];
self.strStrong = nn;
self.strCopy = nn;
[nn appendString:@"kkkk"];
NSLog(@"\nstrStrong的內(nèi)容==%@\nstrCopy的內(nèi)容==%@ \nnnn的內(nèi)容====%@",self.strStrong,self.strCopy,nn);
---輸出
strStrong的內(nèi)容==1234567kkkk
strCopy的內(nèi)容==1234567
nnn的內(nèi)容====1234567kkkk
NSLog(@"\nstrStrong的指針地址 =%p \nstrCopy的指針地址 = %p \nn的指針地址 == %p",self.strStrong, self.strCopy,nn);
---輸出
strStrong的指針地址 =0x60000006b9c0
strCopy的指針地址 = 0xf84349ca70cc38f6
n的指針地址 == 0x60000006b9c0
//strong的指針地址與nn相同,說明指向同一塊內(nèi)存。所以當nn改變的時候strong修飾的對象內(nèi)容會改變。
//copy修飾的對象指針指向的內(nèi)容地址是一塊新的地址,所以修飾string的時候用copy比較合適
所以在希望不改變原始數(shù)據(jù)的情況下使用string串使用copy比較合適
3:修飾array
//copy 與strong對不可變數(shù)組的修飾
@property (nonatomic,strong)NSArray * sArray;
@property (nonatomic,copy)NSArray * cArray;
NSMutableArray * names = [@[@"zahngsan"] mutableCopy];
pers.sArray = names;
pers.cArray = names;
· [names addObject:@"lisi"];
NSLog(@"sArray = %@,cArray = %@",pers.sArray,pers.cArray);
//還是因為 strong 進行了指針拷貝。在內(nèi)存中,兩個變量指向的是同一塊內(nèi)存地址
--輸出
sArray = (
zahngsan,
lisi
),
cArray = (
zahngsan
)
因為使用數(shù)組的時候是要對數(shù)組里的數(shù)據(jù)進行操作的,所以一般使用array用strong修飾
4:copy與mutablecopy
NSString * str1 = @"123";
NSMutableString * str2 = [NSMutableString stringWithString:@"234"];
NSLog(@"str1 = %p str1.copy = %p str1.mutablecopy = %p",str1,str1.copy,str1.mutableCopy);
---輸出
**str1 = 0x108742190 **
**str1.copy = 0x108742190 **
str1.mutablecopy = 0x600002c36fa0
在原始數(shù)據(jù)是不可變數(shù)據(jù)的時候copy指向同一片內(nèi)存地址。
mutablecopy會指向一塊新的內(nèi)存地址
NSLog(@"str2 = %p str2.copy = %p str2.mutablecopy = %p",str2,str2.copy,str2.mutableCopy);
---輸出
**str2 = 0x600002c363a0 **
**str2.copy = 0xb91097712803399a **
str2.mutablecopy = 0x600002c36fa0
原始數(shù)據(jù)是可變數(shù)據(jù)的情況copy與mutablecopy都是新開辟一塊內(nèi)存地址