Objective-C ARC:强与保留、弱与分配
ARC 引入了两个新的内存管理属性:strong
和 weak
。
除了 copy
显然是完全不同的东西之外,strong
与 retain
和 weak
之间还有什么区别吗 code> vs assign
?
根据我的理解,这里唯一的区别是 weak
会将 nil
分配给指针,而 < code>assign 不会,这意味着当我发送消息时程序会崩溃一旦指针被释放。但如果我使用 weak
,这种情况就不会发生,因为发送到 nil
的消息不会执行任何操作。
我不知道 strong
和 retain
之间有什么区别。
我是否有任何理由应该在新项目中使用assign
和retain
,或者这种类型已被弃用?
There are two new memory management attributes for properties introduced by ARC, strong
and weak
.
Apart from copy
, which is obviously something completely different, are there any differences between strong
vs retain
and weak
vs assign
?
From my understanding, the only difference here is that weak
will assign nil
to the pointer, while assign
won't, which means the program will crash when I send a message to the pointer once it's been released. But if I use weak
, this won't ever happen, because message send to nil
won't do anything.
I don't know about any differences between strong
and retain
.
Is there any reason why should I use assign
and retain
in new projects, or are the kind of being deprecated?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
程序来检查可变属性属性后,我决定将所有属性信息放在一起:
下面是详细的文章链接,您可以在其中找到上述所有属性,这肯定会对您有所帮助。
非常感谢所有在这里给出最佳答案的人!
1.strong (iOS4 = keep )
示例:
2.weak
-
示例:
Strong &解释不力,感谢 BJ Homer:
我们什么时候用弱呢?
唯一需要使用weak的时候是如果你想避免引用循环
(例如,父级保留子级,子级保留父级,因此两者都不会被释放)。
3.retain=strong
示例:
4.assign
例子:
After reading so many articles Stackoverflow posts and demo applications to check variable property attributes, I decided to put all the attributes information together:
Below is the detailed article link where you can find above mentioned all attributes, that will definitely help you.
Many thanks to all the people who give best answers here!!
1.strong (iOS4 = retain )
Example:
2.weak
-
Example :
Strong & Weak Explanation, Thanks to BJ Homer:
When we use weak?
The only time you would want to use weak, is if you wanted to avoid retain cycles
(e.g. the parent retains the child and the child retains the parent so neither is ever released).
3.retain = strong
Example:
4.assign
Example:
来自 过渡到 ARC 发行说明(示例中有关属性属性的部分)。
因此,
strong
与属性声明中的retain
相同。对于 ARC 项目,我会使用
strong
而不是retain
,对于 C 基元属性,我会使用assign
,对于 C 基元属性,我会使用weak
对 Objective-C 对象的弱引用。From the Transitioning to ARC Release Notes (the example in the section on property attributes).
So
strong
is the same asretain
in a property declaration.For ARC projects I would use
strong
instead ofretain
, I would useassign
for C primitive properties andweak
for weak references to Objective-C objects.非原子/原子
strong/weak/assign
(可选)
复制
只读
nonatomic/atomic
strong/weak/assign
(Optional)
copy
readonly
据我所知,
strong
和retain
是同义词,因此它们的作用完全相同。那么
weak
几乎就像assign
一样,但是在它指向的对象被释放后自动设置为nil。这意味着,您可以简单地替换它们。
但是,我遇到过一种特殊情况,我必须使用
分配
,而不是比弱
。假设我们有两个属性delegateAssign
和delegateWeak
。两者都存储了我们的委托,即通过拥有唯一的强引用来拥有我们。委托正在释放,因此我们的-dealloc
方法也被调用。委托已处于释放过程中,但仍未完全释放。问题是对他的
weak
引用已经无效!属性delegateWeak
包含nil,但是delegateAssign
包含有效对象(所有属性均已释放并作废,但仍然有效)。这是一个非常特殊的情况,但它向我们揭示了这些弱变量如何工作以及它们何时被取消。
As far as I know,
strong
andretain
are synonyms, so they do exactly the same.Then the
weak
is almost likeassign
, but automatically set to nil after the object, it is pointing to, is deallocated.That means, you can simply replace them.
However, there is one special case I've encountered, where I had to use
assign
, rather thanweak
. Let's say we have two propertiesdelegateAssign
anddelegateWeak
. In both is stored our delegate, that is owning us by having the only strong reference. The delegate is deallocating, so our-dealloc
method is called too.The delegate is already in deallocation process, but still not fully deallocated. The problem is that
weak
references to him are already nullified! PropertydelegateWeak
contains nil, butdelegateAssign
contains valid object (with all properties already released and nullified, but still valid).It is quite special case, but it reveal us how those
weak
variables work and when they are nullified.Clang 关于 Objective-C 自动引用计数 (ARC) 的文档清楚地解释了所有权限定符和修饰符:
然后,声明的财产有六个所有权修饰符:
从语义上看,所有权限定符在读取、赋值、初始化、销毁和移动这五种托管操作中具有不同的含义,其中大多数时候我们只关心赋值操作的差异。
其他在读取、初始化、销毁和移动方面的区别请参考文档中的第4.2节语义。
Clang's document on Objective-C Automatic Reference Counting (ARC) explains the ownership qualifiers and modifiers clearly:
Then there are six ownership modifiers for declared property:
Semantics wise, the ownership qualifiers have different meaning in the five managed operations: Reading, Assignment, Initialization, Destruction and Moving, in which most of times we only care about the difference in Assignment operation.
The other difference in Reading, Init, Destruction and Moving, please refer to Section 4.2 Semantics in the document.
要理解强引用和弱引用,请考虑下面的示例,
假设我们有名为 displayLocalVariable 的方法。
在上述方法中,myName 变量的范围仅限于 displayLocalVariable 方法,一旦该方法完成,保存字符串“ABC”的 myName 变量将从内存中释放。
现在,如果我们想在整个视图控制器生命周期中保留 myName 变量值该怎么办?为此,我们可以创建名为 username 的属性,该属性将对变量 myName 具有强引用(请参见下面代码中的 self.username = myName; ),如下所示,
现在在上面的代码中您可以看到 myName已分配给 self.username 并且 self.username 对 myName 有强引用(正如我们在接口中使用 @property 声明的那样)(间接地对“ABC”字符串有强引用)。因此,在 self.username 存活之前,String myName 不会从内存中释放。
现在考虑将 myName 分配给 dummyName,这是一个弱引用, self.dummyName = myName;
与强引用不同,弱引用只会保留 myName,直到存在对 myName 的强引用为止。
请参阅下面的代码以了解弱引用,
在上面的代码中,有对 myName 的弱引用(即 self.dummyName 对 myName 具有弱引用),但没有对 myName 的强引用,因此 self.dummyName 将无法保存 myName价值。
现在再次考虑下面的代码,
在上面的代码中,self.username 对 myName 具有强引用,因此即使在方法结束后,self.dummyName 现在也将具有 myName 值,因为 myName 具有与其关联的强引用。
现在,每当我们对变量进行强引用时,它的保留计数都会增加 1,并且该变量不会被释放,保留计数达到 0。
希望这会有所帮助。
To understand Strong and Weak reference consider below example,
suppose we have method named as displayLocalVariable.
In above method scope of myName variable is limited to displayLocalVariable method, once the method gets finished myName variable which is holding the string "ABC" will get deallocated from the memory.
Now what if we want to hold the myName variable value throughout our view controller life cycle. For this we can create the property named as username which will have Strong reference to the variable myName(see
self.username = myName;
in below code), as below,Now in above code you can see myName has been assigned to self.username and self.username is having a strong reference(as we declared in interface using @property) to myName(indirectly it's having Strong reference to "ABC" string). Hence String myName will not get deallocated from memory till self.username is alive.
Now consider assigning myName to dummyName which is a Weak reference, self.dummyName = myName;
Unlike Strong reference Weak will hold the myName only till there is Strong reference to myName.
See below code to understand Weak reference,
In above code there is Weak reference to myName(i.e. self.dummyName is having Weak reference to myName) but there is no Strong reference to myName, hence self.dummyName will not be able to hold the myName value.
Now again consider the below code,
In above code self.username has a Strong reference to myName, hence self.dummyName will now have a value of myName even after method ends since myName has a Strong reference associated with it.
Now whenever we make a Strong reference to a variable it's retain count get increased by one and the variable will not get deallocated retain count reaches to 0.
Hope this helps.
强:
示例:
@property(强,非原子)ViewController *viewController;
@synthesize viewController;
Weak
默认情况下会自动获取并设置为 nil
示例:
@property(弱,非原子)IBOutlet UIButton *myButton;
@synthesize myButton;
Strong:
Example:
@property (strong, nonatomic) ViewController *viewController;
@synthesize viewController;
Weak
By Default automatically get and set to nil
Example :
@property (weak, nonatomic) IBOutlet UIButton *myButton;
@synthesize myButton;
Strong和retain的区别:
。弱引用和赋值:
The differences between strong and retain:
The differences between weak and assign: