Objective-C 中的弱属性设置器属性和强属性设置器属性
Objective-C 中弱属性设置器属性和强属性设置器属性有什么区别?
@property(retain, [weak/strong]) __attribute__((NSObject)) CFDictionaryRef myDictionary;
有什么影响和好处?
我听说iOS 4上没有weak,我们需要使用assign。
弱和分配类似吗?
What is the difference between weak and strong property setter attributes in 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.
Is weak similar to assign?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是我对变量属性的了解
所以下面是详细的文章链接,您可以在其中找到以上提到的所有属性,一定会对您有所帮助。
非常感谢所有在这里给出最佳答案的人!
01.强(iOS4 = 保留)
- 它说“将其保留在堆中,直到我不再指向它”
- 换句话说“我是所有者,你不能在与保留相同的目标之前释放它”
- 仅当需要保留对象时才使用强。
- 默认情况下,所有实例变量和局部变量都是强指针。
- 我们通常对 UIViewControllers(UI 项的父级)使用 Strong
- Strong 与 ARC 一起使用,它基本上可以帮助您,因为不必担心对象的保留计数。当您使用完毕后,ARC 会自动为您释放它。使用关键字 Strong 意味着您拥有该对象。
示例:
02.弱(iOS4 = unsafe_unretained)
- 它说“只要其他人强烈指出它,就保留它”
- 与分配相同,没有保留或释放
- “弱”参考是指您不保留的参考。
- 我们通常对 IBOutlets(UIViewController 的 Childs)使用弱。这有效,因为仅子对象
只要父对象存在,就需要存在。
- 弱引用是不能保护引用对象不被垃圾收集器收集的引用。
- 弱本质上是指派,一种未保留的财产。除了当对象被释放时,弱指针会自动设置为 nil
示例:
解释:感谢 BJ Homer
想象我们的对象是一只狗,并且狗想要逃跑(被释放)。
强指针就像是狗身上的皮带。只要你给狗拴上皮带,狗就不会逃跑。如果五个人将皮带拴在一只狗身上(五个强力指针指向一个物体),那么在所有五个皮带都松开之前,狗不会逃跑。
另一方面,弱指针就像小孩子指着狗说“看!一只狗!”只要狗仍然拴在皮带上,小孩子仍然可以看到狗,他们仍然会指着它。然而,一旦所有的皮带都被松开,无论有多少小孩子指着它,狗都会逃跑。
一旦最后一个强指针(约束)不再指向某个对象,该对象将被释放,并且所有弱指针将被清零。
我们什么时候用弱呢?
唯一需要使用weak的时候是如果你想避免引用循环
(例如,父级保留子级,子级保留父级,因此两者都不会被释放)。
Here is what I know about variable properties
so 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 have given the 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'm 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.
Example:
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
Example :
Explain:Thanks to 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).
对于特定文件,您可以打开或关闭 ARC。如果它打开,则无法使用
retain
release
autorelease
等...而是使用strong
weak< /code> 属性或
__strong
__weak
用于变量(默认为__strong
)。 Strong 相当于保留,但 ARC 会为您管理发布。唯一需要使用weak的情况是,如果您想避免保留循环(例如,父级保留子级,子级保留父级,因此两者都不会被释放)。
“免费桥接”部分(从
NS
到CF
的转换)有点棘手。您仍然需要手动管理 CF 对象的CFRelease()
和CFRetain()
。当你将它们转换回 NS 对象时,你必须告诉编译器保留计数,以便它知道你做了什么。一切都在这里。
You either have ARC on or off for a particular file. If its on you cannot use
retain
release
autorelease
etc... Instead you usestrong
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.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
toCF
) is a little tricky. You still have to manually manageCFRelease()
andCFRetain()
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.Its all here.
列出 Robert 引用的文档中明确回答您最后两个问题的部分:
这称为归零弱引用。您可以使用 __unsafe_unretained 创建不将弱引用归零的弱引用,但顾名思义,通常不建议这样做。
也在文档中:
To call out the parts of the docs referenced by Robert that answer your last two questions explicitly:
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.
Also in the docs:
WEAK属性的清晰使用如下:
Crystal clear use of WEAK property is as follows:
让我们举个例子来详细说明一下(上面的答案已经很好了),这个例子可能会有更多帮助
让我们有两个类 A 和 B
上面的代码将生成一个保留循环,因为它们都是强类型
a----->b--------->a
因此为了避免它,您必须使用其中之一的 week 属性,以便它每周引用
对象而不增加它的引用计数。
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
the above code will generate a retain cycle because both are the strong type
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.