一颤殴、數(shù)據(jù)源是NSMutableArray類型
@property(nonatomic, copy) NSMutableArray *aCopyMutArray;
@property(nonatomic, copy) NSArray *aCopyArray;
@property(nonatomic, strong) NSMutableArray *aStrongMutArray;
@property(nonatomic, strong) NSArray *aStrongArray;
首先創(chuàng)建了四個(gè)不同類型的數(shù)組對象
并賦上相同的值,可變型的數(shù)組
NSMutableArray *testArray = [NSMutableArray arrayWithObjects:@"1",@"2", nil];
self.aCopyArray = testArray;
self.aStrongArray = testArray;
self.aCopyMutArray = testArray;
self.aStrongMutArray = testArray;
[testArray addObject:@"3"];
NSLog(@"=================step 1 = =========");
[self printArrays];
2018-03-12 16:44:33.839259+0800 Nunca[10196:1294488] =================step 1 = =========
2018-03-12 16:44:33.839567+0800 Nunca[10196:1294488] testArray = (
1,
2,
3
),isa = 0x600000246ed0
2018-03-12 16:44:33.839729+0800 Nunca[10196:1294488] aCopyArray = (
1,
2
),isa = 0x600000229b80
2018-03-12 16:44:33.839962+0800 Nunca[10196:1294488] aStrongArray = (
1,
2,
3
),isa = 0x600000246ed0
2018-03-12 16:44:33.840212+0800 Nunca[10196:1294488] aCopyMutArray = (
1,
2
),isa = 0x60000022a5a0
2018-03-12 16:44:33.840343+0800 Nunca[10196:1294488] aStrongMutArray = (
1,
2,
3
),isa = 0x600000246ed0
改變testArray的值鼻忠,可見以strong修飾的對象指針指向的是同testArray同一塊內(nèi)存涵但,而以copy修飾的對象則被分配了新的內(nèi)存
NSLog(@"=================step 2 = =========");
if ([self.aCopyMutArray isKindOfClass:[NSMutableArray class]]) {
[self.aCopyMutArray addObject:@"4"];
NSLog(@"aCopyMutArray.class = NSMutableArray");
} else {
NSLog(@"aCopyMutArray.class = NSArray");
}
if ([self.aStrongArray isKindOfClass:[NSMutableArray class]]) {
void (*imp) (id,SEL,id) = (void (*)(id,SEL,id))[self.aStrongArray methodForSelector:@selector(addObject:)];
imp(self.aStrongArray,@selector(addObject:),@"4");
NSLog(@"aStrongArray.class = NSMutableArray");
} else {
NSLog(@"aStrongArray.class = NSArray");
}
[self printArrays];
2018-03-12 16:44:33.840434+0800 Nunca[10196:1294488] =================step 2 = =========
2018-03-12 16:44:33.840638+0800 Nunca[10196:1294488] aCopyMutArray.class = NSArray
2018-03-12 16:44:33.840850+0800 Nunca[10196:1294488] aStrongArray.class = NSMutableArray
2018-03-12 16:44:33.841007+0800 Nunca[10196:1294488] aCopyArray = (
1,
2
),isa = 0x600000229b80
2018-03-12 16:44:33.841099+0800 Nunca[10196:1294488] aStrongArray = (
1,
2,
3,
4
),isa = 0x600000246ed0
2018-03-12 16:44:33.841277+0800 Nunca[10196:1294488] aCopyMutArray = (
1,
2
),isa = 0x60000022a5a0
2018-03-12 16:44:33.841462+0800 Nunca[10196:1294488] aStrongMutArray = (
1,
2,
3,
4
),isa = 0x600000246ed0
aCopyMutArray 雖然注明的是NSMutableArray類型,但由于copy屬性帖蔓,實(shí)際上是不可變類型矮瘟,aStrongArray雖然標(biāo)明是NSArray類型,但由于strong屬性塑娇,實(shí)際上的真實(shí)類型是跟數(shù)據(jù)源(testArray)保持一致澈侠。
二、數(shù)據(jù)源是NSArray類型
NSArray *testArray = [NSArray arrayWithObjects:@"1",@"2", nil];
self.aCopyArray = testArray;
self.aStrongArray = testArray;
self.aCopyMutArray = [NSMutableArray arrayWithArray:testArray];
self.aStrongMutArray = [NSMutableArray arrayWithArray:testArray];
NSLog(@"=================step 1 = =========");
[self printArrays];
2018-03-12 16:59:21.242880+0800 Nunca[10595:1312829] =================step 1 = =========
2018-03-12 16:59:21.243138+0800 Nunca[10595:1312829] testArray = (
1,
2
),isa = 0x60000023b7a0
2018-03-12 16:59:21.243740+0800 Nunca[10595:1312829] aCopyArray = (
1,
2
),isa = 0x60000023b7a0
2018-03-12 16:59:21.244087+0800 Nunca[10595:1312829] aStrongArray = (
1,
2
),isa = 0x60000023b7a0
2018-03-12 16:59:21.244433+0800 Nunca[10595:1312829] aCopyMutArray = (
1,
2
),isa = 0x60000002f780
2018-03-12 16:59:21.244684+0800 Nunca[10595:1312829] aStrongMutArray = (
1,
2
),isa = 0x600000243240
可見埋酬,當(dāng)數(shù)據(jù)源是不可變類型時(shí)哨啃,不管對象是用strong還是用copy修飾,都會指向數(shù)據(jù)源同一塊內(nèi)存写妥,不會再新創(chuàng)建
NSLog(@"=================step 2 = =========");
if ([self.aCopyMutArray isKindOfClass:[NSMutableArray class]]) {
[self.aCopyMutArray addObject:@"4"];
NSLog(@"aCopyMutArray.class = NSMutableArray");
} else {
NSLog(@"aCopyMutArray.class = NSArray");
}
if ([self.aStrongMutArray isKindOfClass:[NSMutableArray class]]) {
[self.aStrongMutArray addObject:@"4"];
NSLog(@"aStrongMutArray.class = NSMutableArray");
} else {
NSLog(@"aStrongMutArray.class = NSArray");
}
2018-03-12 16:59:21.244815+0800 Nunca[10595:1312829] =================step 2 = =========
2018-03-12 16:59:21.244989+0800 Nunca[10595:1312829] aCopyMutArray.class = NSArray
2018-03-12 16:59:21.245375+0800 Nunca[10595:1312829] aStrongMutArray.class = NSMutableArray
以copy修飾的aCopyMutArray仍然是不可變類型
結(jié)論:
- 當(dāng)源數(shù)據(jù)與想持有的對象都是NSArray類型時(shí)拳球,直接用strong或copy修飾都可以,效果也是一樣耳标;
- 當(dāng)源數(shù)據(jù)為NSMutableArray類型醇坝,想持有的對象為NSArray時(shí),用copy修飾次坡;
- 當(dāng)源數(shù)據(jù)為NSArray或NSMutableArray類型呼猪,想持有的對象為NSMutableArray時(shí),用strong修飾對象并通過 [NSMutableArray arrayWithArray:testArray] 賦值砸琅,即可保證對象的可變性又能擺脫源數(shù)據(jù)變化時(shí)帶來的影響宋距。