ios Objective-C 中的弱属性 setter 属性和强属性 setter 属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7912555/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Weak and strong property setter attributes in Objective-C
提问by kkurni
What is the difference between weak and strong property setter attributes in Objective-C?
Objective-C 中的弱属性设置器属性和强属性设置器属性有什么区别?
@property(retain, [weak/strong]) __attribute__((NSObject)) CFDictionaryRef myDictionary;
What is the impact and benefit?
有什么影响和好处?
I heard that weak is not available on iOS 4 and we need to use assign.
我听说弱在 iOS 4 上不可用,我们需要使用分配。
Is weak similar to assign?
弱类似于分配吗?
回答by swiftBoy
Here is information what I know about variable properties
以下是我对变量属性的了解
- atomic //default
- nonatomic
- strong=retain //default
- weak
- retain
- assign //default
- unsafe_unretained
- copy
- readonly
- readwrite //default
- 原子 //默认
- 非原子的
- 强=保留//默认
- 虚弱的
- 保持
- 赋值 //默认
- unsafe_unretained
- 复制
- 只读
- 读写 //默认
so below is the detailed article link where you can find above mentioned all attributes, that will defiantly help you. Many thanks to all the people who give best answers here!!
所以下面是详细的文章链接,您可以在其中找到上述所有属性,这将对您有所帮助。非常感谢所有在这里给出最佳答案的人!!
01.strong (iOS4 = retain )- it says "keep this in the heap until I don't point to it anymore" - in other words " I'am the owner, you cannot dealloc this before aim fine with that same as retain" - You use strong only if you need to retain the object. - By default all instance variables and local variables are strong pointers. - We generally use strong for UIViewControllers (UI item's parents) - strong is used with ARC and it basically helps you , by not having to worry about the retain count of an object. ARC automatically releases it for you when you are done with it.Using the keyword strong means that you own the object.
01. strong (iOS4 = retain )- 它说“将它保留在堆中,直到我不再指向它为止” - 换句话说,“我是所有者,你不能在瞄准之前解除分配它与保留相同" - 只有在需要保留对象时才使用 strong。- 默认情况下,所有实例变量和局部变量都是强指针。- 我们通常对 UIViewControllers(UI 项的父项)使用 strong - strong 与 ARC 一起使用,它基本上可以帮助您,不必担心对象的保留计数。当您完成它时,ARC 会自动为您释放它。使用关键字 strong 表示您拥有该对象。
Example:
例子:
@property (strong, nonatomic) ViewController *viewController;
@synthesize viewController;
02.weak (iOS4 = unsafe_unretained )- it says "keep this as long as someone else points to it strongly" - the same thing as assign, no retain or release - A "weak" reference is a reference that you do not retain. - We generally use weak for IBOutlets (UIViewController's Childs).This works because the child object only needs to exist as long as the parent object does. - a weak reference is a reference that does not protect the referenced object from collection by a garbage collector. - Weak is essentially assign, a unretained property. Except the when the object is deallocated the weak pointer is automatically set to nil
02.弱 (iOS4 = unsafe_unretained )- 它说“只要其他人强烈地指向它就保持它” - 与赋值相同,没有保留或释放 - “弱”引用是您不保留的引用。- 我们通常对IBOutlets(UIViewController 的Childs)使用weak。这是因为子对象只需要在父对象存在时就存在。- 弱引用是不保护被引用对象不被垃圾收集器收集的引用。- 弱本质上是赋值,一种未保留的属性。除了对象被释放时,弱指针自动设置为 nil
Example :
例子 :
@property (weak, nonatomic) IBOutlet UIButton *myButton;
@synthesize myButton;
Explain:Thanks to BJ Homer
解释:感谢 BJ Homer
Imagine our object is a dog, and that the dog wants to run away (be deallocated). Strong pointers are like a leash on the dog. As long as you have the leash attached to the dog, the dog will not run away. If five people attach their leash to one dog, (five strong pointers to one object), then the dog will not run away until all five leashes are detached. Weak pointers, on the other hand, are like little kids pointing at the dog and saying "Look! A dog!" As long as the dog is still on the leash, the little kids can still see the dog, and they'll still point to it. As soon as all the leashes are detached, though, the dog runs away no matter how many little kids are pointing to it. As soon as the last strong pointer (leash) no longer points to an object, the object will be deallocated, and all weak pointers will be zeroed out. 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).
想象一下我们的对象是一只狗,而这只狗想要逃跑(被释放)。强指针就像狗的皮带。只要你把皮带拴在狗身上,狗就不会逃跑。如果五个人将他们的皮带拴在一只狗上(五个强烈的指针指向一个物体),那么在所有五根皮带都松开之前,狗不会逃跑。另一方面,弱指针就像小孩子指着狗说“看!一只狗!” 只要狗仍然被拴在皮带上,小孩子仍然可以看到狗,他们仍然会指向它。然而,一旦所有的皮带都松开了,不管有多少小孩指着它,这只狗都会逃跑。一旦最后一个强指针(leash)不再指向一个对象,该对象将被释放,所有弱指针将被清零。当我们使用弱?您唯一想要使用弱的情况是,如果您想避免保留循环(例如,父项保留子项而子项保留父项,因此两者都不会被释放)。
回答by Robert
You either have ARC on or off for a particular file. If its on you cannot use retain
release
autorelease
etc... Instead you use strong
weak
for properties or __strong
__weak
for variables (defaults to __strong
). Strong is the equivalent to retain, however ARC will manage the release for you.
您可以为特定文件打开或关闭 ARC。如果它打开,你不能使用retain
release
autorelease
等......相反,你使用strong
weak
属性或__strong
__weak
变量(默认为__strong
)。Strong 相当于保留,但是 ARC 会为您管理发布。
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).
您唯一想要使用弱的情况是,如果您想避免保留循环(例如,父项保留子项而子项保留父项,因此两者都不会被释放)。
The 'toll free bridging' part (casting from NS
to CF
) is a little tricky. You still have to manually manage CFRelease()
and CFRetain()
for CF objects. When you convert them back to NS objects you have to tell the compiler about the retain count so it knows what you have done.
“免费桥接”部分(从NS
to投射CF
)有点棘手。您仍然需要手动管理CFRelease()
和CFRetain()
对CF的对象。当您将它们转换回 NS 对象时,您必须告诉编译器有关保留计数的信息,以便它知道您做了什么。
Its all here.
它的全部在这里。
回答by Alen Alexander
Crystal clear use of WEAK property is as follows:
WEAK 属性的清晰使用如下:
Any control whose properties we need to change(eg:text of a label) is declared weak and as below:
@property(nonatomic,weak) IBOutlet Type *name;
Eg: @property(nonatomic,weak) IBOutlet UILabel *myLabel;
回答by rimsky
To call out the parts of the docs referenced by Robert that answer your last two questions explicitly:
要调用 Robert 引用的文档中明确回答您最后两个问题的部分:
// The following declaration is similar to "@property(assign) MyClass *myObject;"
// except that if the MyClass instance is deallocated,
// the property value is set to nil instead of remaining as a dangling pointer.
@property(weak) MyClass *myObject;
This is referred to as a zeroing weak reference. You can create weak references that are not zeroing weak references using __unsafe_unretained, but as the name implies, this is generally not recommended.
这称为归零弱参考。您可以使用 __unsafe_unretained 创建不将弱引用归零的弱引用,但顾名思义,通常不建议这样做。
Also in the docs:
同样在文档中:
Weak references are not supported in Mac OS X v10.6 and iOS 4.
回答by Anurag Bhakuni
let take an example to elaborate more(above answer are already great), may this example helps little more
让我们举个例子来详细说明(上面的答案已经很好了),这个例子可能有更多帮助
let we have two class A and B
让我们有两个类 A 和 B
//A.h
#import <Foundation/Foundation.h>
#import "B.h"
@interface A : NSObject
@property (nonatomic, strong) B *objB;
@end
@implementation A
//
@end
//B.h
#import <Foundation/Foundation.h>
#import "A.h"
@interface B : NSObject
@property strong text(nonatomic, strong) A *objA;
@end
@implementation B
//
@end
and in main
#import "B.h"
#import "A.h"
{
A *obja =[[A alloc]init];
B *objb =[[B alloc]init];
A.objB=objb;
B.objA=obja;
}
the above code will generate a retain cycle because both are the strong type a-------->b--------->a
上面的代码会产生一个retain循环,因为都是强类型a-------->b--------->a
so to avoid it you have to use week property of one of it so that it weekly refer to the object and not increase it reference count.
所以为了避免它,你必须使用其中之一的 week 属性,以便它每周引用该对象而不是增加它的引用计数。