OC中copy修饰@property
1.copy修饰字符串(NSString和NSMutableString)
copy修饰字符串,可以防止外界修改内部的数据
@interface Student : NSObject
@property (nonatomic, strong) NSString * name;
@end
NSMutableString *temp = [[NSMutableString alloc]initWithFormat:@"lwl"];
Student *stu = [[Student alloc]init];
stu.name = temp;
[temp appendString:@" test"];
NSLog(@"name = %@",stu.name);
首先我们创建了一个Student类,并声明了一个name属性,修饰符为strong。在代码二中,定义了一个变量temp可变字符串,并创建了stu实例。把temp字符串赋值给stu中的name属性。最后temp字符串追加@“test”,最后打印出来的stu的name也会追加test;
2019-01-27 14:50:25.484899+0800 CopyUserProperty[8486:938202] Hello, World!
2019-01-27 14:50:25.485079+0800 CopyUserProperty[8486:938202] name = lwl test
Program ended with exit code: 0
**解决办法**
只需要将Student中的name属性修饰的strong变成copy即可
@interface Student : NSObject
@property (nonatomic, copy) NSString * name;
@end
2019-01-27 15:12:39.131612+0800 CopyUserProperty[9016:1066999] Hello, World!
2019-01-27 15:12:39.131878+0800 CopyUserProperty[9016:1066999] name = lwl
Program ended with exit code: 0
**分析原因**
如果修饰符为strong,在将temp赋值给stu的name的时候,temp指向的对象会在引用计数(retaincount)会加1,相单于两个变量同时指向了同一个对象,修改其中一个另外一个肯定会变换。如果修饰符为copy,在将temp赋值给stu的name的时候,会重新拷贝一个对象放在堆中,temp指向的对象的引用计数(retaincount)不会加1,temp和stu的name指向不同的对象。是深拷贝。
2.copy修饰block
block默认存储在栈中,栈中的block访问到外界的对象时,不会进行相应的retain操作;如果block在堆中(Block_copy宏可以将block转移到堆中),在访问外界的对象时,会进行相应的retain操作。
Student *stu = [[Student alloc]init];
NSLog(@"retainCount = %lu",[stu retainCount]);
void (^myBlock)(int) = ^(int age){
NSLog(@"num = %@",stu);
};
myBlock(3);
Block_copy(myBlock);
NSLog(@"retainCount = %lu",[stu retainCount]);
Student类新增block属性
typedef void (^myBlock)(void);
@interface Student : NSObject
@property (nonatomic, assign) myBlock pBlock;
@property (nonatomic, copy) NSString * name;
@end
Dog *d = [[Dog alloc]init];
Student *stu = [[Student alloc]init];
stu.pBlock = ^{
NSLog(@"d = %@",d);
};
[d release];
stu.pBlock();
[stu release];
在stu对象中的block中访问到d对象,因为block修饰的属性是assgin,block保存在栈中,所以对d对象的引用计数(retaincount)不会加1,d对象释放之后,stu对用了block就会访问到僵尸对象,导致程序崩溃。
**解决方法**
typedef void (^myBlock)(void);
@interface Student : NSObject
*注意:如果是block使用copy并不是拷贝,而是转移*
@property (nonatomic, copy) myBlock pBlock;
@property (nonatomic, copy) NSString * name;
@end
将block修饰符变为copy的时候,block会被转移到堆中,会对其引用的对象增加引用计数。
3.copy修饰block的循环引用问题
2019-01-27 15:51:57.716059+0800 CopyUserProperty[10115:1259494] d = <Dog: 0x1005168e0>
2019-01-27 15:51:57.716220+0800 CopyUserProperty[10115:1259494] -[Student dealloc]
Program ended with exit code: 0
上面的代码中Student对象可以释放,但dog对象没有释放。
**解除方法**
只需要在block使用的对象前面加上__block修饰
__block Dog *d = [[Dog alloc]init];
Student *stu = [[Student alloc]init];
stu.pBlock = ^{
NSLog(@"d = %@",d);
};
[d release];
stu.pBlock();
[stu release];
2019-01-27 16:04:59.728277+0800 CopyUserProperty[10441:1288724] -[Dog dealloc]
2019-01-27 16:04:59.728615+0800 CopyUserProperty[10441:1288724] d = <Dog: 0x100682b30>
2019-01-27 16:04:59.728632+0800 CopyUserProperty[10441:1288724] -[Student dealloc]
Program ended with exit code: 0