@property (nonatomic,strong) NSArray *array1;
@property (nonatomic,copy) NSArray *array2;
@property (nonatomic,strong) NSMutableArray *array3;
@property (nonatomic,copy) NSMutableArray *array4;
//NSString、NSArray、NSDictionary 等等经常使用copy关键字,是因为他们有对应的可变类型:NSMutableString、NSMutableArray、NSMutableDictionary;
NSArray *arrayTem1 = @[@"1",@"2",@"3",@"4",@"5"];
NSMutableArray *arrayTem2 = [NSMutableArray arrayWithArray:arrayTem1];
//strong 指针赋值,如果被赋的原始值被改动,则自己也被改动
self.array1 = arrayTem2;
NSLog(@"%@",self.array1); // 1 2 3 4 5
[arrayTem2 addObject:@"6"];//原始值增加元素 自己也是
NSLog(@"%@",self.array1); // 1 2 3 4 5 6
//copy 拷贝一份值,原始值的改变不会影响自己
self.array2 = arrayTem2;
NSLog(@"%@",self.array2);// 1 2 3 4 5 6
[arrayTem2 addObject:@"7"];
NSLog(@"%@",self.array2);// 1 2 3 4 5 6
//strong 同上
self.array3 = arrayTem2; //1234567
NSLog(@"%@",self.array3);//1 2 3 4 5 6 7
[arrayTem2 addObject:@"8"];
NSLog(@"%@",self.array3);//1 2 3 4 5 6 7 8
[self.array3 addObject:@"array3"];
//copy copy属性是拷贝一份不可变的对象 所以自己也不受影响
self.array4 = arrayTem2;//12345678
NSLog(@"%@",self.array4);//1 2 3 4 5 6 7 8
[arrayTem2 addObject:@"9"];
NSLog(@"%@",self.array4);// 1 2 3 4 5 6 7 8
//这时自己已经是不可变的了,执行增删操作都会crash
[self.array4 addObject:@"array4"];//crash
网友评论