[Objective-C]strong、copy在NSArray和NSMutableArray的應用區別
阿新 • • 發佈:2018-12-11
兩個問題
1. 使用strong修飾NSArray會有什麼問題?
2. 使用copy修飾NSMutableArray會有什麼問題?
1. 使用strong修飾NSArray的問題
//
// StrongCopyTest.h
// StudyRuntime
//
// Created by Andy Liu on 2018/5/14.
// Copyright © 2018年 Andy Liu. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface StrongCopyTest : NSObject
@property ( nonatomic,strong) NSArray *immutableArray;
@property (nonatomic,strong) NSMutableArray *mutableArray;
- (void)doTest;
@end
//
// StrongCopyTest.m
// StudyRuntime
//
// Created by Andy Liu on 2018/5/14.
// Copyright © 2018年 Andy Liu. All rights reserved.
//
#import "StrongCopyTest.h"
@implementation StrongCopyTest
- (void)doTest
{
self.mutableArray = [NSMutableArray arrayWithObject:@"111"];
self.immutableArray = [NSArray array];
NSLog(@"before mutableArray=%@,immutableArray=%@",self.mutableArray,self.immutableArray);
self.immutableArray = self.mutableArray;
[self.mutableArray addObject: @"222"];
NSLog(@"after mutableArray=%@,immutableArray=%@",self.mutableArray,self.immutableArray);
}
@end
輸出結果如下
2018-05-14 17:28:15.872188+0800 StudyRuntime[861:40479] before mutableArray=(
111
),immutableArray=(
)
2018-05-14 17:28:15.872308+0800 StudyRuntime[861:40479] after mutableArray=(
111,
222
),immutableArray=(
111,
222
)
Program ended with exit code: 0
可以看出,immutabeArray和mutableArray都被修改了,immutableArray不是預料的結果。
原因分析:
self.immutableArray = self.mutableArray;
這行程式碼將可變陣列的指標賦值給了不可變陣列指標,由於OC中都是C的指標操作,並且是弱型別,因此這是合法的,編譯器不會報錯。甚至一個 id x = self.mutableArray也是可以的,因此弱型別是有潛在風險的。此時immutableArray和mutableArray都是指向了同一個記憶體地址,即mutableArray的記憶體地址,因此後面修改mutableArray的資料也會導致immutableArray的資料修改,因為是同一塊記憶體地址。 這是一個陷阱。
2. 使用copy修飾NSMutableArray會有什麼問題?
//
// StrongCopyTest.h
// StudyRuntime
//
// Created by Andy Liu on 2018/5/14.
// Copyright © 2018年 Andy Liu. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface StrongCopyTest : NSObject
//@property (nonatomic,copy) NSArray *immutableArray;
@property (nonatomic,copy) NSMutableArray *mutableArray;
- (void)doTest;
@end
//
// StrongCopyTest.m
// StudyRuntime
//
// Created by Andy Liu on 2018/5/14.
// Copyright © 2018年 Andy Liu. All rights reserved.
//
#import "StrongCopyTest.h"
@implementation StrongCopyTest
- (void)doTest
{
self.mutableArray = [NSMutableArray arrayWithObject:@"111"];
// self.immutableArray = [NSArray array];
//
// NSLog(@"before mutableArray=%@,immutabxrleArray=%@",self.mutableArray,self.immutableArray);
//
// self.immutableArray = self.mutableArray;
[self.mutableArray addObject:@"222"];
//NSLog(@"after mutableArray=%@,immutableArray=%@",self.mutableArray,self.immutableArray);
}
@end
輸出結果如下
2018-05-14 17:59:37.794905+0800 StudyRuntime[944:61946] -[__NSSingleObjectArrayI addObject:]: unrecognized selector sent to instance 0x10061c540
2018-05-14 17:59:37.796059+0800 StudyRuntime[944:61946] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSSingleObjectArrayI addObject:]: unrecognized selector sent to instance 0x10061c540'
*** First throw call stack:
(
0 CoreFoundation 0x00007fff2a17032b __exceptionPreprocess + 171
1 libobjc.A.dylib 0x00007fff517eac76 objc_exception_throw + 48
2 CoreFoundation 0x00007fff2a208e04 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
3 CoreFoundation 0x00007fff2a0e6870 ___forwarding___ + 1456
4 CoreFoundation 0x00007fff2a0e6238 _CF_forwarding_prep_0 + 120
5 StudyRuntime 0x0000000100001b91 -[StrongCopyTest doTest] + 161
6 StudyRuntime 0x0000000100001a85 testStrongCopy + 53
7 StudyRuntime 0x0000000100001ad5 main + 53
8 libdyld.dylib 0x00007fff52404015 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
原因分析:
self.mutableArray = [NSMutableArray arrayWithObject:@“111”];這句程式碼將得到一個不可變的陣列(copy屬性導致的).在此處打一個斷點,然後輸出其資料型別:
(lldb) po self.mutableArray
<__NSSingleObjectArrayI 0x102a01ed0>(
111
)
後面的異常就顯而易見了。
self.mutableArray = [NSMutableArray arrayWithObject:@"111"];
這句話相當於
NSMutableArray *arr = [NSMutableArray arrayWithObject:@"111"];
self.mutableArray = [arr copy];
正確的程式碼
@property (nonatomic,copy) NSArray *immutableArray;
@property (nonatomic,strong) NSMutableArray *mutableArray;
其他集合
NSArray,NSDictionary,NSSet -> copy
NSMutableArray,NSMutableDictionary,NSMutableSet->strong
``