代碼1使用 「_」 做任務:的iOS保留副本問題
@interface ViewController()
@property (nonatomic,retain) NSMutableString *rrstr;
@property (nonatomic,copy) NSMutableString *copystr;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSMutableString *ssss = [[NSMutableString alloc]initWithString:@"ddddd"];
_rrstr = ssss;
_copystr = ssss;
NSLog(@"%@===%@===%@",ssss,self.rrstr,self.copystr);
[ssss appendString:@"1231"];
NSLog(@"%@===%@===%@",ssss,self.rrstr,self.copystr);
[ssss deleteCharactersInRange:NSMakeRange(1, 3)];
NSLog(@"%@===%@===%@",ssss,self.rrstr,self.copystr);
}
代碼1輸出: 「」
2016-09-19 10:44:29.190 retin[1160:72426] ddddd===ddddd===ddddd
2016-09-19 10:44:29.191 retin[1160:72426] ddddd1231===ddddd1231===ddddd1231
2016-09-19 10:44:29.192 retin[1160:72426] dd1231===dd1231===dd1231
碼2使用做任務:
@interface ViewController()
@property (nonatomic,retain) NSMutableString *rrstr;
@property (nonatomic,copy) NSMutableString *copystr;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSMutableString *ssss = [[NSMutableString alloc]initWithString:@"ddddd"];
self.rrstr = ssss;
self.copystr = ssss;
NSLog(@"%@===%@===%@",ssss,self.rrstr,self.copystr);
[ssss appendString:@"1231"];
NSLog(@"%@===%@===%@",ssss,self.rrstr,self.copystr);
[ssss deleteCharactersInRange:NSMakeRange(1, 3)];
NSLog(@"%@===%@===%@",ssss,self.rrstr,self.copystr);
}
碼2輸出:
2016-09-19 10:50:09.079 retin[1190:75922] ddddd===ddddd===ddddd
2016-09-19 10:50:09.079 retin[1190:75922] ddddd1231===ddddd1231===ddddd
2016-09-19 10:50:09.080 retin[1190:75922] dd1231===dd1231===ddddd
我很困惑,爲什麼這兩個代碼具有不同的輸出?這些代碼不應該有相同的輸出嗎? 「_」和「。」之間的差異究竟是什麼?句法?
更好地發佈代碼,而不是屏幕截圖 – Leo