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;

相关参考

NSString什么时候用copy,什么时候用strong

时间: 2024-08-03 15:12:06

iOS中属性 (nonatomic, copy, strong, weak)的使用 By hL的相关文章

对于atomic nonatomic assign retain copy strong weak的简单理解

atomic和nonatomic用来决定编译器生成的getter和setter是否为原子操作 1)atomic 设置成员变量的@property属性时,atomic是默认值,提供多线程安全 在多线程环境下,原子操作是必要的,否则有可能引起错误的结果.加了atomic后setter函数会变成下面这样: {lock} if (property != newValue) { [property release]; property = [newValue retain]; } {unlock} 2)n

IOS中Retain和Copy的区别

IOS中Retain和Copy的区别 1 ,可读性: readonly  . readwrite@property(readwrite,....) valueType value;这个属性是变量的默认属性,就是如果你 (readwrite and readonly 都没有使用,那么你的变量就是 readwrite 属性 ) ,通过加入 readwrite 属性你的变量就会有 get 方法,和 set 方法.property(readonly,...) valueType value;这个属性变量

iOS中assign、copy 、retain等关键字的含义

assign: 简单赋值,不更改索引计数copy: 建立一个索引计数为1的对象,然后释放旧对象 retain:释放旧的对象,将旧对象的值赋予输入对象,再提高输入对象的索引计数为1 Copy其实是建立了一个相同的对象,而retain不是:比如一个NSString对象,地址为0×1111,内容为@"STR" Copy到另外一个NSString之 后,地址为0×2222,内容相同,新的对象retain为1, 旧有对象没有变化 retain到另外一个NSString之 后,地址相同(建立一个指

iOS中assign,copy,retain之间的区别以及weak和strong的区别(面试)

• copy: 用于希望保持一份传入值的拷贝,而不是值自身的情况,即把原来的对象完整的赋值到另外一地方,重新加载一内存区,一个地方变了不影响另一个地方的对象. • assign:  简单的直接赋值,相当于说两个对象指向同个内存区,一个地方的变了,其他的也跟着改变. • retain:释放旧的对象,将旧对象的值赋予输入对象,再提高输入对象的索引计数 为1 那上面的是什么意思呢? Copy 其实是建立了一个相同的对象,而 retain 不是: 比如一个 NSString 对象,地址为 0×1111,

atomic nonatomic assign retain copy strong weak 介绍

atomic和nonatomic用来决定编译器生成的getter和setter是否为原子操作.         atomic 设置成员变量的@property属性时,默认为atomic,提供多线程安全. 在多线程环境下,原子操作是必要的,否则有可能引起错误的结果.加了atomic,setter函数会变成下面这样:                        {lock}                                if (property != newValue) {    

探讨一下iOS中深拷贝&浅拷贝&copy的那些事儿

什么是深拷贝?什么是浅拷贝? 为什么经常看到字符串属性要这样定义,那个copy是神马意思? @property(nonatomic,copy)NSString* name; 为什么下面的写法是错误的? @property(nonatomic,copy)NSMutableString* name; copyWithZone方法又到底是干嘛用的? 接下来,我们将一起,一步一步的去揭晓问题的答案. Copy到底是个啥? 其实我们真的没必要把copy想的太高深.它之所以叫copy,其终极目的已不言而喻,

iOS中assign,copy,retain之间的区别以及weak和strong的区别

@property (nonatomic, assign) NSString *title; 什么是assign,copy,retain之间的区别? assign: 简单赋值,不更改索引计数(Reference Counting). copy: 建立一个索引计数为1的对象,然后释放旧对象 retain:释放旧的对象,将旧对象的值赋予输入对象,再提高输入对象的索引计数为1 weak 和strong的区别: (weak和strong)不同的是 当一个对象不再有strong类型的指针指向它的时候 它会

assign,copy,strong,weak,nonatomic的具体理解

例子: NSString *houseOfMM = [[NSString alloc] initWithString:'MM的三室两厅']; 上面一段代码会执行以下两个动作:  1 在堆上分配一段内存用来存储@' MM的三室两厅 ' ,比如:内存地址为 0X1111  内容为 ' MM的三室两厅' ,  2 在栈上分配一段内存用来存储 houseForWife ,比如:地址为 0XAAAA  内容自然为 0X1111 下面分别看下(assign,retain,copy):  1.assign的情

assign,copy,strong,weak,nonatomic的理解

举个例子: NSString *houseOfMM = [[NSString alloc] initWithString:'MM的三室两厅']; 上面一段代码会执行以下两个动作:  1 在堆上分配一段内存用来存储@' MM的三室两厅 ' ,比如:内存地址为 0X1111  内容为 ' MM的三室两厅' ,  2 在栈上分配一段内存用来存储 houseForWife ,比如:地址为 0XAAAA  内容自然为 0X1111 下面分别看下(assign,retain,copy):  1.assign