OC语言-NSMutableArray为什么要用strong来修饰

来源:互联网 发布:淘宝详情页面设计 编辑:程序博客网 时间:2024/06/08 15:01
Talk is cheap show you my code! 
NSMutableArray属性为什么要用strong来修饰,其实就是一个深复制和浅复制的问题。
<pre name="code" class="objc">#import "ViewController.h"@interface ViewController ()@property (nonatomic, copy) NSMutableArray *array;@property(nonatomic,copy) NSMutableArray *arrayCopy;@property(nonatomic,strong) NSMutableArray *arrayStrong;@end@implementation ViewController- (NSMutableArray *)array {    if (_array == nil) {        _array = [NSMutableArray array];    }    return _array;}- (void)viewDidLoad {    [super viewDidLoad];        // NSMutableArray 为什么要strong的原因,此处会崩溃!因为调用的是set方法,copy会拷贝一个不可变的副本,再为其添加对象便会崩溃!    self.arrayCopy = [NSMutableArray array];    [self.arrayCopy addObject:@"copy"];    self.arrayStrong=[NSMutableArray arrayWithArray:@[@"strong",@"strong"]];    [self.arrayStrong addObject:@"strong"];    }- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {    // NSMutableArray 此时,调用的是get方法,get方法实例化了一个可变的对象,此时是可以再为其添加对象的,这个需要需要记住!    [self.array addObject:@"copy"];    NSLog(@"%@",self.array);}@end


                                             
0 0