Copy

2017-05-16  本文已影响0人  夜雨聲煩_
概述

用@property声明 NSString、NSArray、NSDictionary 经常使用copy关键字,是因为他们有对应的可变类型:NSMutableString、NSMutableArray、NSMutableDictionary,他们之间可能进行赋值操作,为确保对象中的字符串值不会无意间变动,应该在设置新属性值时拷贝一份。

Copy与字符串

copy在面对nsstring时和strong一样
但copy在面对nsmutablestring时和srong有所区别 copy做的是深拷贝 创建了新的地址 而strong只是单纯的地址引用

@interface ViewController ()

@property (strong, nonatomic) NSMutableString *initialStr;
@property (strong, nonatomic) NSString *normalStr;
@property (copy, nonatomic) NSString *aCopyStr;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.initialStr = [NSMutableString stringWithString:@"初始"];
    self.normalStr = self.initialStr;
    self.aCopyStr = self.initialStr;
    [self.initialStr replaceCharactersInRange:NSMakeRange(0, 2) withString:@"变化"];
    NSLog(@"initialStr:%@,normalStr:%@,coryStr:%@",self.initialStr,self.normalStr,self.aCopyStr);
    //打印结果为:initialStr:变化,normalStr:变化,coryStr:初始
    
    
   self.initialStr = (NSMutableString*)@"变化";
    NSLog(@"initialStr:%@,normalStr:%@,coryStr:%@",self.initialStr,self.normalStr,self.aCopyStr);
    //打印结果为:initialStr:变化,normalStr:初始,coryStr:初始
}

使用replace或者append对NSMutableString的值进行改变的时候,改变的是intitialStr地址内的值,此时浅拷贝normalStr跟随变化,修饰词为copy的深拷贝字符串copyStr不跟随变化。而使用@""赋值相当于赋予新的内存地址,此时无论深拷贝还是浅拷贝均指向初始地址,值不变。
以下两种深拷贝效果一样:

@property (copy, nonatomic) NSString *aCopyStr;
self.aCopyStr = self.initialStr;

@property (strong, nonatomic) NSString *aCopyStr;
self.aCopyStr = [self.initialStr mutableCopy];
Copy与Block

block属性声明使用copy
block中使用本地变量如self,若使用assign会使block放在栈上,当栈被释放时本地变量将不可访问,会bad access
而声明copy后,在block使用self会造成循环引用,因此要转成weak。详见Block章节说明。
使用如下:

//1
__weak CurrentViewController* blockSelf = self;
//2
__weak __typeof__(self) weakSelf = self
//3
@weakify(self)

但是在block中如果执行完[self dosomething]后self可能会被置为nil,因此在之后[self dootherthing]时会出错
所以:如果在 Block内需要多次访问self,则需要使用strongSelf。

//1
__strong __typeof(self) strongSelf = weakSelf 
//2
@strongify(self)
上一篇下一篇

猜你喜欢

热点阅读