iOS中属性 (nonatomic, copy, strong, weak)的使用 By hL
以下内容来自Stackflow的详解
1.Nonatomic
nonatomic is used for multi threading purposes. If we have set the nonatomic attribute at the time of declaration, then any other thread wanting access to that object can access it and give results in respect to multi-threading.
2.Copy
copy is required when the object is mutable. Use this if you need the value of the object as it is at this moment, and you don't want that value to reflect any changes made by other owners of the object. You will need to release the object when you are finished with it because you are retaining the copy.
常用于(block,NSString)
3.Assign
Assign is somewhat the opposite to copy. When calling the getter of an assign property, it returns a reference to the actual data. Typically you use this attribute when you have a property of primitive type (float, int, BOOL...)
常用于简单数据类型(float, int, NSInteger,BOOL,SEL...)
4.Retain
retain is required when the attribute is a pointer to an object. The setter generated by @synthesize will retain (aka add a retain count to) the object. You will need to release the object when you are finished with it. By using retain it will increase the retain count and occupy memory in autorelease pool.
5.Strong
strong is a replacement for the retain attribute, as part of Objective-C Automated Reference Counting (ARC). In non-ARC code it's just a synonym for retain.
Retain与strong是等价的
如 MBProgressHUD.h 中的宏定义
#ifndef MB_STRONG
#if __has_feature(objc_arc)
#define MB_STRONG strong
#else
#define MB_STRONG retain
#endif
#endif
6.Weak
weak is similar to strong except that it won't increase the reference count by 1. It does not become an owner of that object but just holds a reference to it. If the object's reference count drops to 0, even though you may still be pointing to it here, it will be deallocated from memory.
The above link contain both Good information regarding Weak and Strong.
From Stackflow
手思中第三方库AMSmoothAlertView中的相关属性写法参考
关于NSString在MRC时代经常用Copy,Retain属性,而Retain==Strong,所以ARC中很多项目申明为
@property (nonatomic,strong) NSString *userName;
参考第2条的Copy介绍,使用 Copy更安全
@property (nonatomic,Copy) NSString *userName;
相关参考
iOS中属性 (nonatomic, copy, strong, weak)的使用 By hL的更多相关文章
- 对于atomic nonatomic assign retain copy strong weak的简单理解
atomic和nonatomic用来决定编译器生成的getter和setter是否为原子操作 1)atomic 设置成员变量的@property属性时,atomic是默认值,提供多线程安全 在多线程环 ...
- iOS中assign、copy 、retain等关键字的含义
iOS中assign.copy .retain等关键字的含义 转自:http://my.oschina.net/majiage/blog/267409 assign: 简单赋值,不更改索引计数cop ...
- iOS知识基础篇--@property,@synthesize, nonatomic,atomic,strong,weak,copy,assign,retain详解
一.@property 这个关键词的唯一作用就是声明getter.setter方法接口. 二.@synthesize 实现setter.getter方法,找不到实例变量则主动创建一个. 三.nonat ...
- iOS中assign,copy,retain之间的区别以及weak和strong的区别(面试)
• copy: 用于希望保持一份传入值的拷贝,而不是值自身的情况,即把原来的对象完整的赋值到另外一地方,重新加载一内存区,一个地方变了不影响另一个地方的对象. • assign: 简单的直接赋值,相 ...
- @property中的copy.strong.weak总结
1.NSString类型的属性为什么用copy NSString类型的属性可以用strong修饰,但会造成一些问题,请看下面代码 #import "ViewController.h" ...
- ios 内存管理与property copy strong weak assign
- (void)fun{ NSString* str = [[NSString alloc] initWithString:@"string"]; NSLog(@"% ...
- iOS中属性Property的常用关键字的使用说明
属性关键字的作用 现在我们iOS开发中,基本都是使用ARC(自动引用计数)技术,来编写我们的代码.因此在属性property中我们经常使用的关键字有strong,weak,assign,copy,no ...
- iOS中assign,copy,retain之间的区别以及weak和strong的区别
@property (nonatomic, assign) NSString *title; 什么是assign,copy,retain之间的区别? assign: 简单赋值,不更改索引计数(Refe ...
- assign,copy,strong,weak,nonatomic的理解
举个例子: NSString *houseOfMM = [[NSString alloc] initWithString:'MM的三室两厅']; 上面一段代码会执行以下两个动作: 1 在堆上分配一段 ...
随机推荐
- 制造运营管理 (MOM) 的工作流驱动方法
介绍 "在企业中使用的任何技术的第一条规则是,应用于高效运营的自动化将放大效率.第二个是自动化应用于低效率的操作会放大低效率." - 比尔盖茨 . 工作流是结构化的活动,主要涉及人 ...
- ProtoBuf3语法指南(Protocol Buffers)_下
0.说明 ProtoBuf3语法指南, 又称为proto3, 是谷歌的Protocol Buffers第3个版本. 本文基于官方英文版本翻译, 加上了自己的理解少量修改, 一共分为上下两部分. 1.A ...
- SpringBoot 之 整合JDBC使用
导入相关依赖: # pom.xml <dependency> <groupId>org.springframework.boot</groupId> <art ...
- mongdb分片
实验环境 主机 IP 虚拟通道 centos1 192.168.3.10 vmnet8 centos2 ...
- Selenium_使用switch_to.alert处理弹窗(14)
与switch_to.window 和 switch_to.frame 相比,switch_to.alert的alert方法使用了@property 装饰器,所以在使用时alert被当成属性调用. 演 ...
- Nacos配置管理最佳实践
Nacos一个最常用的功能就是配置中心,在具体使用时往往是多个团队,甚至整个公司的研发团队都使用同一个Nacos服务.那么使用时如何保证配置在各个团队之间的隔离,又能保证配置管理的便捷性?下面就来介绍 ...
- python中的sort方法和sorted方法
一.sort()函数 描述 sort() 函数用于对原列表进行排序,如果指定参数,则使用比较函数指定的比较函数. 语法 sort()方法语法: 1 list.sort(cmp=None, key=No ...
- 源代码管理git地址从http改为https,提交400错误
推送400错误 cmd 执行 git config --global http.sslVerify false 推送地址,修改http 为 https 就可以正常提交了
- [USB波形分析] 全速USB波形数据分析(三)
前面的两篇文章介绍和分析了USB的一些基本知识,结合前面的介绍,今天用实例介绍USB的枚举过程. 1 | 概况 硬件基于EK-TMC123GXL开发板,软件是TI提供的USB批量传输的简单例子,在PC ...
- virtual stuido同时调试多个控制台
问题 UDP作业需要服务器端和客户端收发信息完成交互,需要同时调试多个窗口. 解决办法 但是缺点依然是无法调试2个,修改另一个测试. 所以多开可能依然是好办法.