iOS NSString属性时使用copy和strong的区别

OC 同时被 3 个专栏收录
6 篇文章 0 订阅
5 篇文章 0 订阅
1 篇文章 0 订阅

原字符串为不可变的情况NSString
这个情况下分别用cop和strong修饰原字符串,测试字符串分别为可变字符串
NSMutableString 和不可变字符串NSString,同时由copy和strong修饰;

@interface ViewController ()
@property (nonatomic, copy)NSString *immutableStrCopy;
@property (nonatomic, strong)NSString *immutableStrStrong;
@property (nonatomic, copy)NSMutableString *variableStrCopy;
@property (nonatomic, strong)NSMutableString *variableStrStrong;

@property (nonatomic, copy)NSString *immutableTestStrCopy;
@property (nonatomic, strong)NSString *immutableTestStrStrong;
@property (nonatomic, copy)NSMutableString *variableTestStrCopy;
@property (nonatomic, strong)NSMutableString *variableTestStrStrong;
@end
@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.immutableStrCopy = @"111";
    self.immutableTestStrCopy = self.immutableStrCopy;
    self.immutableTestStrStrong = self.immutableStrCopy;
    self.variableTestStrCopy = self.immutableStrCopy;
    self.variableTestStrStrong = self.immutableStrCopy;
    NSLog(@"\n variableStrStrong = %@ - variableStrStrong的地址:%p\n immutableTestStrCopy = %@ - immutableTestStrCopy的地址:%p\n immutableTestStrStrong = %@ - immutableTestStrStrong的地址:%p\n variableTestStrCopy = %@ - variableTestStrCopy的地址:%p\n variableTestStrStrong = %@ - variableTestStrStrong的地址:%p",self.variableStrStrong,_variableStrStrong,self.immutableTestStrCopy,_immutableTestStrCopy,self.immutableTestStrStrong,_immutableTestStrStrong,self.variableTestStrCopy,_variableTestStrCopy,self.variableTestStrStrong,_variableTestStrStrong);
    
    self.immutableStrCopy = @"222";
    
    NSLog(@"\n variableStrStrong = %@ - variableStrStrong的地址:%p\n immutableTestStrCopy = %@ - immutableTestStrCopy的地址:%p\n immutableTestStrStrong = %@ - immutableTestStrStrong的地址:%p\n variableTestStrCopy = %@ - variableTestStrCopy的地址:%p\n variableTestStrStrong = %@ - variableTestStrStrong的地址:%p",self.variableStrStrong,_variableStrStrong,self.immutableTestStrCopy,_immutableTestStrCopy,self.immutableTestStrStrong,_immutableTestStrStrong,self.variableTestStrCopy,_variableTestStrCopy,self.variableTestStrStrong,_variableTestStrStrong);
}
@end

在这里插入图片描述

被copy修饰的不可变字符NSString发生改变时不会引起测试数据改变,原因是:原数据赋值给测试数据是赋值的是地址,当改变原数据时相当于重新创建了字符对象,并没有改变原来的地址

- (void)viewDidLoad {
    [super viewDidLoad];
    self.immutableStrStrong = @"111";
    self.immutableTestStrCopy = self.immutableStrStrong;
    self.immutableTestStrStrong = self.immutableStrStrong;
    self.variableTestStrCopy = self.immutableStrStrong;
    self.variableTestStrStrong = self.immutableStrStrong;
    NSLog(@"\n variableStrStrong = %@ - variableStrStrong的地址:%p\n immutableTestStrCopy = %@ - immutableTestStrCopy的地址:%p\n immutableTestStrStrong = %@ - immutableTestStrStrong的地址:%p\n variableTestStrCopy = %@ - variableTestStrCopy的地址:%p\n variableTestStrStrong = %@ - variableTestStrStrong的地址:%p",self.variableStrStrong,_variableStrStrong,self.immutableTestStrCopy,_immutableTestStrCopy,self.immutableTestStrStrong,_immutableTestStrStrong,self.variableTestStrCopy,_variableTestStrCopy,self.variableTestStrStrong,_variableTestStrStrong);
    
    self.immutableStrStrong = @"222";
    
    NSLog(@"\n variableStrStrong = %@ - variableStrStrong的地址:%p\n immutableTestStrCopy = %@ - immutableTestStrCopy的地址:%p\n immutableTestStrStrong = %@ - immutableTestStrStrong的地址:%p\n variableTestStrCopy = %@ - variableTestStrCopy的地址:%p\n variableTestStrStrong = %@ - variableTestStrStrong的地址:%p",self.variableStrStrong,_variableStrStrong,self.immutableTestStrCopy,_immutableTestStrCopy,self.immutableTestStrStrong,_immutableTestStrStrong,self.variableTestStrCopy,_variableTestStrCopy,self.variableTestStrStrong,_variableTestStrStrong);
}

在这里插入图片描述
可以看到NSString被copy和strong修饰的运行结果完全一样,都是因为原数据赋值给测试数据赋值的是地址,当改变原数据时相当于重新创建了字符对象,并没有改变原来的地址

原数据为可变字符NSMutableString

[self.variableStrCopy appendString:@“222”];
以追加的方式修改原地址的内容

self.variableStrCopy = [NSMutableString stringWithString:@“111”];
给不可变字符赋值相当于重新创建个字符

- (void)viewDidLoad {
    [super viewDidLoad];
    self.variableStrCopy = [NSMutableString stringWithString:@"111"];
    self.immutableTestStrCopy = self.variableStrCopy;
    self.immutableTestStrStrong = self.variableStrCopy;
    self.variableTestStrCopy = self.variableStrCopy;
    self.variableTestStrStrong = self.variableStrCopy;
    NSLog(@"\n variableStrStrong = %@ - variableStrStrong的地址:%p\n immutableTestStrCopy = %@ - immutableTestStrCopy的地址:%p\n immutableTestStrStrong = %@ - immutableTestStrStrong的地址:%p\n variableTestStrCopy = %@ - variableTestStrCopy的地址:%p\n variableTestStrStrong = %@ - variableTestStrStrong的地址:%p",self.variableStrStrong,_variableStrStrong,self.immutableTestStrCopy,_immutableTestStrCopy,self.immutableTestStrStrong,_immutableTestStrStrong,self.variableTestStrCopy,_variableTestStrCopy,self.variableTestStrStrong,_variableTestStrStrong);
    
     self.variableStrCopy = [NSMutableString stringWithString:@"222"];
    
    NSLog(@"\n variableStrStrong = %@ - variableStrStrong的地址:%p\n immutableTestStrCopy = %@ - immutableTestStrCopy的地址:%p\n immutableTestStrStrong = %@ - immutableTestStrStrong的地址:%p\n variableTestStrCopy = %@ - variableTestStrCopy的地址:%p\n variableTestStrStrong = %@ - variableTestStrStrong的地址:%p",self.variableStrStrong,_variableStrStrong,self.immutableTestStrCopy,_immutableTestStrCopy,self.immutableTestStrStrong,_immutableTestStrStrong,self.variableTestStrCopy,_variableTestStrCopy,self.variableTestStrStrong,_variableTestStrStrong);
}

在这里插入图片描述
当原数据为可变字符串NSMutableString时 并用copy修饰时 [self.variableStrCopy appendString:@“222”]以追加的方式修改原地址的内容不能用,原因是:可变字符调用copy时会创建个不可变字符,现在的原数据类型相当于不可变字符(NSString)copy只是在set跟get 方法里实现,如果通过懒加载方式加载属性则是可以用的

只能通过self.variableStrCopy = [NSMutableString stringWithString:@“111”]修改原数据,但是测试数据的值不会发生变化,原因是原数据赋值给测试数据赋值的是地址,当改变原数据时相当于重新创建了字符对象,并没有改变原来的地址的内容

- (void)viewDidLoad {
    [super viewDidLoad];
    self.variableStrStrong = [NSMutableString stringWithString:@"111"];
    self.immutableTestStrCopy = self.variableStrStrong;
    self.immutableTestStrStrong = self.variableStrStrong;
    self.variableTestStrCopy = self.variableStrStrong;
    self.variableTestStrStrong = self.variableStrStrong;
    NSLog(@"\n variableStrStrong = %@ - variableStrStrong的地址:%p\n immutableTestStrCopy = %@ - immutableTestStrCopy的地址:%p\n immutableTestStrStrong = %@ - immutableTestStrStrong的地址:%p\n variableTestStrCopy = %@ - variableTestStrCopy的地址:%p\n variableTestStrStrong = %@ - variableTestStrStrong的地址:%p",self.variableStrStrong,_variableStrStrong,self.immutableTestStrCopy,_immutableTestStrCopy,self.immutableTestStrStrong,_immutableTestStrStrong,self.variableTestStrCopy,_variableTestStrCopy,self.variableTestStrStrong,_variableTestStrStrong);
    
//     self.variableStrCopy = [NSMutableString stringWithString:@"222"];
    [self.variableStrStrong appendString:@"222"];
    
    NSLog(@"\n variableStrStrong = %@ - variableStrStrong的地址:%p\n immutableTestStrCopy = %@ - immutableTestStrCopy的地址:%p\n immutableTestStrStrong = %@ - immutableTestStrStrong的地址:%p\n variableTestStrCopy = %@ - variableTestStrCopy的地址:%p\n variableTestStrStrong = %@ - variableTestStrStrong的地址:%p",self.variableStrStrong,_variableStrStrong,self.immutableTestStrCopy,_immutableTestStrCopy,self.immutableTestStrStrong,_immutableTestStrStrong,self.variableTestStrCopy,_variableTestStrCopy,self.variableTestStrStrong,_variableTestStrStrong);
}

可以看到被copy修饰的测试数据,内容没有发生改变但是地址发生了改变;原因是:可变字符调用copy时会创建个不可变字符
所以被copy修饰的测试数据是重新创建了个不可变字符对像赋值,而被strong修饰的则是直接赋值的原数据的地址当原地址上的内容发生改变时,测试数据的内容也就改变了;

- (void)viewDidLoad {
    [super viewDidLoad];
    self.variableStrStrong = [NSMutableString stringWithString:@"111"];
    self.immutableTestStrCopy = self.variableStrStrong;
    self.immutableTestStrStrong = self.variableStrStrong;
    self.variableTestStrCopy = self.variableStrStrong;
    self.variableTestStrStrong = self.variableStrStrong;
    NSLog(@"\n variableStrStrong = %@ - variableStrStrong的地址:%p\n immutableTestStrCopy = %@ - immutableTestStrCopy的地址:%p\n immutableTestStrStrong = %@ - immutableTestStrStrong的地址:%p\n variableTestStrCopy = %@ - variableTestStrCopy的地址:%p\n variableTestStrStrong = %@ - variableTestStrStrong的地址:%p",self.variableStrStrong,_variableStrStrong,self.immutableTestStrCopy,_immutableTestStrCopy,self.immutableTestStrStrong,_immutableTestStrStrong,self.variableTestStrCopy,_variableTestStrCopy,self.variableTestStrStrong,_variableTestStrStrong);
    
     self.variableStrCopy = [NSMutableString stringWithString:@"222"];
//    [self.variableStrStrong appendString:@"222"];
    
    NSLog(@"\n variableStrStrong = %@ - variableStrStrong的地址:%p\n immutableTestStrCopy = %@ - immutableTestStrCopy的地址:%p\n immutableTestStrStrong = %@ - immutableTestStrStrong的地址:%p\n variableTestStrCopy = %@ - variableTestStrCopy的地址:%p\n variableTestStrStrong = %@ - variableTestStrStrong的地址:%p",self.variableStrStrong,_variableStrStrong,self.immutableTestStrCopy,_immutableTestStrCopy,self.immutableTestStrStrong,_immutableTestStrStrong,self.variableTestStrCopy,_variableTestStrCopy,self.variableTestStrStrong,_variableTestStrStrong);
}

在这里插入图片描述
因为可变变字符调用copy时会创建个不可变字符所以被copy修饰的测试数据是重新创建了个不可变字符对像赋值,所以地址发生了改变,而被strong修饰的则是直接赋值的原数据的地址当原地址上的内容没有改变时,测试数据的内容也就不会改变了;

总结:字符串是否会发生改变在于是否改变了字符串所指向地址的内容是否发生改变,可以使字符串所指向地址的内容是发生改变的只有NSMutableString 所以NSMutableString一定要用copy修饰(如不希望测试数据随原数据改变)否则被strong修饰的字符串会随原数据改变而改变 ,而NSString原则上用copy和strong效果一样,但为了防止被NSMutableString修改还是要用copy

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

<p> <span style="color:#337FE5;"><strong>【为什么还需要学习C++?】</strong></span> </p> <p style="margin-left:0cm;"> 你是否接触很多语言,但从来没有了解过编程语言的本质? </p> <p style="margin-left:0cm;text-align:start;"> 你是否想成为一名资深开发人员,想开发别人做不了的高性能程序? </p> <p style="margin-left:0cm;text-align:start;"> 你是否经常想要窥探大型企业级开发工程的思路,但苦于没有基础只能望洋兴叹? </p> <p style="margin-left:0cm;text-align:start;">   </p> <p style="margin-left:0cm;text-align:start;"> 那么C++就是你个人能力提升,职业之路进阶的不二之选。 </p> <p style="margin-left:0cm;text-align:start;"> <br /> </p> <p style="margin-left:0cm;text-align:start;"> <br /> </p> <p style="margin-left:0cm;"> <strong><span style="color:#337FE5;">【课程特色】</span></strong> </p> <p style="margin-left:0cm;text-align:start;"> 1.课程共19大章节,239课内容,涵盖数据结构、函数、类、指针、标准库全部知识体系。 </p> <p style="margin-left:0cm;text-align:start;"> 2.带你从知识与思想的层面从0构建C++知识框架,分析大型项目实践思路,为你打下坚实的基础。 </p> <p style="margin-left:0cm;text-align:start;"> 3.李宁老师结合4大国外顶级C++著作的精华为大家推出的《征服C++11》课程。 </p> <p style="margin-left:0cm;text-align:start;"> <br /> </p> <p class="ql-long-24357476"> <span style="color:#337FE5;"><strong>【学完后我将达到什么水平?】</strong></span> </p> <p class="ql-long-24357476"> 1.对C++的各个知识能够熟练配置、开发、部署; </p> <p class="ql-long-24357476"> 2.吊打一切关于C++的笔试面试题; </p> <p class="ql-long-24357476"> 3.面向物联网的“嵌入式”面向大型化的“分布式”开发,掌握职业钥匙,把握行业先机。 </p> <p class="MsoNoSpacing" style="margin-left:18pt;"> <br /> </p> <div> <br /> </div> <p> <br /> </p> <p style="margin-left:0cm;text-align:start;"> <span style="color:#337FE5;"><strong>【面向人群】</strong></span> </p> <p style="margin-left:0cm;text-align:start;"> <span style="color:#222226;font-family:PingFangSC-Regular, "font-size:14px;background-color:#FFFFFF;">1.希望一站式快速入门的C++初学者;</span> </p> <p style="margin-left:0cm;text-align:start;"> <span style="color:#222226;font-family:PingFangSC-Regular, "font-size:14px;background-color:#FFFFFF;">2.希望快速学习 C++、掌握编程要义、修炼内功的开发者;</span> </p> <p style="margin-left:0cm;text-align:start;"> <span style="color:#222226;font-family:PingFangSC-Regular, "font-size:14px;background-color:#FFFFFF;">3.有志于挑战更高级的开发项目,成为资深开发的工程师。</span> </p> <p style="margin-left:0cm;text-align:start;"> <br /> </p> <p> <br /> </p> <p> <span style="color:#337FE5;"><strong>【课程设计】</strong></span> </p> <p> 本课程包含3大模块 </p> <p> <strong>基础篇</strong><br /> 本篇主要讲解c++的基础概念,包含数据类型、运算符等基本语法,数组、指针、字符串等基本词法,循环、函数、类等基本句法等。 </p> <p> <br /> <strong>进阶篇</strong><br /> 本篇主要讲解编程中常用的一些技能,包含类的高级技术、类的继承、编译链接命名空间等。 </p> <p> <br /> <strong>提升篇:</strong><br /> 本篇可以帮助学员更加高效的进行c++开发,其中包含类型转换、文件操作、异常处理、代码重用等内容。 </p> <p> <img src="https://img-bss.csdnimg.cn/202007091130239667.png" alt="" /> </p>
©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值