@property attributes 比较
来源:互联网 发布:天音淘宝复制大师3.03 编辑:程序博客网 时间:2024/04/30 00:33
常用的 Attributes 有:
- 读写属性 (readwrite/readonly)
- setter语意 (assign/retain/copy/strong/weak)
- 原子性 (atomic/nonatomic)
assign 是直接赋值,当变量release 时, 该声明的成员变量将会无效.
retain/strong 是在赋值时,将变量的引用计数 +1, 当此类释放此成员变量时,该变量的引用计数 -1
copy 当前类持有该成员变量,与其它变量的内存变量不一样
assign 和 retain 的区别
简单的说, 当你把变量 A 赋值给 属性为 assign 的变量assignStr 时候, A的引用计数没有增加,这样一来虽然 assignStr 引用了 变量A ,它的计数根本没有增加,这样当变量A 被自动释放时,这个时候 assignStr 就会报错. 另外是 retain, 当把变量A 赋值给 属性为 retain(strong) 的变量strongStr 时候, A的引用计数+1了,这样只要 strongStr 没有被释放,A 是不会被释放的,即任何时候调用 strongStr 都是没有问题的.
下面的代码是测试 assign 和 retain 的区别:
1. 变量声明
2. 调用 createStr 方法分别给两个方法赋值
3. 输出 strongStr 和 assignStr的值,结果是输出 assignStr 时报错
注意: 如果直接 调用 createStr 然后再调用 timeAction 这样是不会报错的,因为两个方法在同一个 runloop 里面,也就是说在createStr 方法里面的str1 和 str2 没有被释放,这样的话输出时当然不会有问题了. 使用 @autoreleasepool 或者 开一个新的线程就是为了提前释放 str1 和 str2 以达到我们测试的效果.
变量声明
@property(strong,nonatomic)NSString*stongStr;
@property(assign,nonatomic)NSString*assignStr;
相关输出log方法
- (void)createStr {
NSString *str1 = [[NSStringalloc]initWithFormat:@"stong str%d",arc4random() % 11];
NSString *str2 = [[NSStringalloc]initWithFormat:@"assign str%d",arc4random() % 11];
self.stongStr= str1;
self.assignStr= str2;
NSLog(@"%@",self.stongStr);
NSLog(@"%@",self.assignStr);
}
- (void)timeAction {
NSLog(@"%@",self.stongStr);
NSString *str1 = [[NSStringalloc]initWithFormat:@"stong str%d",arc4random() % 11];
NSString *str2 = [[NSStringalloc]initWithFormat:@"assign str%d",arc4random() % 11];
self.stongStr= str1;
self.assignStr= str2;
NSLog(@"%@",self.stongStr);
NSLog(@"%@",self.assignStr);
}
- (void)timeAction {
NSLog(@"%@",self.stongStr);
NSLog(@"%@",self.assignStr);//输出报错
}
相关测试方法
@autoreleasepool{
[selfcreateStr];
}
[selfcreateStr];
}
[selftimeAction];
或
[selfcreateStr];
dispatch_async(dispatch_get_main_queue(), ^{
[selftimeAction];
dispatch_async(dispatch_get_main_queue(), ^{
[selftimeAction];
});
strong 与retain (apple 官方解释)
(https://developer.apple.com/library/mac/releasenotes/ObjectiveC/RN-TransitioningToARC/Introduction/Introduction.html)
— 完 2015年7月22日 下午4:57
0 0
- @property attributes 比较
- Property attributes
- @property (attributes) 迷惑的attributes
- Property Declaration Attributes
- Reading Property Attributes
- Variable property attributes or Modifiers in iOS
- Variable property attributes or Modifiers in iOS
- Variable property attributes or Modifiers in iOS
- Objective-C declared @property attributes (nonatomic, copy, strong, weak)
- Object-c:Property Attributes && Variable life time Qualifiers
- IOS Dev Intro - Variable property attributes or Modifiers in iOS
- Unity Editor 基础篇(七):Property Attributes自定义属性
- Attributes
- Attributes
- $$Attributes
- jquery中.prop()和.attr()的区别 什么叫布尔型属性 property vs attributes , boolean attributes...
- html 中的属性(property)以及DOM对象中的attributes的详解
- 文本属性Attributes-也是多文本的拓展感觉比较好分享给大家
- UIWindow 和 ViewController理解
- 【HDU 5316】Magician(线段树)
- 多个jquery{}在html中的执行顺序
- Hadoop学习笔记(Map-Reduce的工作、调度机制)
- 简易聊天室五 用户登录
- @property attributes 比较
- 带方向状态的搜索
- 关于初学Java注意事项一
- Socket发起post请求,并保持session的状态
- Java构造和解析Json数据的两种方法详解一
- Python之集合(set)
- 多线程入门教程一:基本概念
- java线程池中任务执行完成后再次执行
- 得到Windows资源