前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >IOS @proporty 关键字(一)retain strong

IOS @proporty 关键字(一)retain strong

作者头像
FlyLolo
发布2018-05-17 15:27:12
5430
发布2018-05-17 15:27:12
举报
文章被收录于专栏:Core Net
代码语言:javascript
复制
@interface User : NSObject  
  
@property (nonatomic,retain) NSString* tRetain;  
@property (nonatomic,assign) NSString* tAssign;  
@property (nonatomic,copy) NSString* tcopy;  
  
@end  

类User有个属性tRetain, 只是测试就用NSString类型了(此类型一般用copy, 因为可能是个NSMutableString,不希望在赋值后被其他地方修改内容)。

代码语言:javascript
复制
User* user = [[User alloc]init];  
  
NSString* testRetain = [NSString stringWithFormat:@"retain"];  
NSLog(@"testRetain.retainCount=%lu",(unsigned long)testRetain.retainCount);  //1  
  
user.tRetain = testRetain;  
NSLog(@"testRetain.retainCount=%lu",(unsigned long)testRetain.retainCount);   //2  
NSLog(@"user.tRetain.retainCount=%lu",(unsigned long)user.tRetain.retainCount); //2  
  
NSString* testRetain2 = @"retain2";  
NSLog(@"testRetain2.retainCount=%lu",(unsigned long)testRetain2.retainCount); //4294967295 自动释放对象  返回max unsigned long  
  
  
user.tRetain = testRetain2;  
NSLog(@"testRetain.retainCount=%lu",(unsigned long)testRetain.retainCount);   //1  
NSLog(@"testRetain2.retainCount=%lu",(unsigned long)testRetain2.retainCount); //4294967295  
NSLog(@"user.RetainValue.retainCount=%lu",(unsigned long)user.tRetain.retainCount); //4294967295  
  
  
NSString* testRetain3 = [NSString stringWithFormat: @"retain3"];  
NSLog(@"testRetain3.retainCount=%lu",(unsigned long)testRetain3.retainCount); //1  
  
[testRetain3 retain];  
NSLog(@"testRetain3.retainCount=%lu",(unsigned long)testRetain3.retainCount); //2  
  
NSString* testRetain4 = [NSString stringWithString:testRetain3];  
NSLog(@"testRetain3.retainCount=%lu",(unsigned long)testRetain3.retainCount); //3  
NSLog(@"testRetain4.retainCount=%lu",(unsigned long)testRetain4.retainCount); //3  

strong是ARC后引入的关键字, 在ARC环境中等同于Retain。

NSSring* str = [NSString stringWithString:字符串];   此方法相当于上文对一个retain属性赋值。   若后面的字符串参数的计数为4294967295,则str的计数也是。   若字符串参数可计数, 例如1, 则执行后计数加1.

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2015-04-28 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档