简体   繁体   English

Objective-C属性关键字

[英]Objective-C Property Keywords

OK - newbie Objective-C question: 好的 - 新手Objective-C问题:

When declaring properties there are attributes such as below: 声明属性时,有如下属性:

@property (weak, nonatomic)

I realize I need read up on this to fully understand it but most of what I found was reference material, so a link to a good article that could explain best practices / usage scenarios (when to use which attribute for primitives, reference types, outlets, etc) or a couple of examples would be appreciated. 我意识到我需要阅读这篇文章才能完全理解它,但我发现的大部分内容都是参考资料,所以链接到一篇好文章,可以解释最佳实践/使用场景(何时使用哪些属性用于基元,参考类型,出口)等等)或者几个例子将不胜感激。

Thanks! 谢谢!

Even though it is waaaay to late for an answer i've found this question while googleing the same issue and also found this article by apple which explains the whole thing perfectly. 尽管回答我已经找到了这个问题,但是谷歌相同的问题,并且还发现苹果的这篇文章完全解释了整个事情。

Hope it helps to anyone who are researching the same thing, 希望对正在研究同样事物的人有所帮助,

http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/ProgrammingWithObjectiveC/EncapsulatingData/EncapsulatingData.html http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/ProgrammingWithObjectiveC/EncapsulatingData/EncapsulatingData.html

From a recent class I gave on this (inspired by Paul Hegarty) 从我最近的课程(灵感来自Paul Hegarty)

nonatomic - NOT thread safe see link Justin pointed out in the comments above. nonatomic - NOT thread safe看到链接Justin在上面的评论中指出

strong (or retain) - keep this object allocated until I don't point to it anymore (set it to nil). 强(或保留) - 保持此对象分配,直到我不再指向它(将其设置为nil)。 Compiler will also throw this out of the heap (deallocate it) if I am not pointed to strongly anymore (I get dealloc'd) 编译器也会把它抛出堆(释放它)如果我不再强烈指向(我得到dealloc'd)

weak - keep this object allocated as long as something still points to it strongly. weak - 只要有一些东西仍然强烈指向它,就保持这个对象的分配。 IBOutlets are usually declared as weak since they will be retained automatically by the view controller. IBOutlets通常被声明为弱,因为它们将由视图控制器自动保留。

Primitive types are not allocated on the heap and don't use strong or weak 原始类型不在堆上分配,也不使用强或弱

Atomicity has to do with threading, and is a pretty advanced topic for a newbie. Atomicity与线程有关,对于新手而言是一个非常高级的主题。 The short answer, however, is that iOS properties are always declared as nonatomic. 然而,简短的回答是iOS属性总是被声明为非原子的。 Here's some more detailed information about it. 这里有一些关于它的更详细的信息

The weak/strong keyword has to do with memory management with ARC and preventing what's called a retain cycle. 弱/强关键字与ARC的内存管理有关,可以防止所谓的保留周期。 This can also be a bit of a tough concept for a newbie, but the high-level overview is that a retain cycle happens when two objects have strong references to each other and thus neither object will be destroyed by ARC. 对于新手来说,这也可能是一个棘手的概念,但是高级概述是当两个对象彼此具有强引用时发生保留循环,因此ARC不会破坏任何对象。 This is a form of a memory leak, as you may have an object that's no longer in use but is still taking up memory. 这是一种内存泄漏的形式,因为您可能有一个不再使用但仍占用内存的对象。 By declaring a property as weak, it will ensure that it's not automatically destroyed so long as something still has a strong reference to it. 通过将属性声明为弱,它将确保它不会自动销毁,只要某些东西仍然具有强烈的引用。 For instance, let's say you have an array with a couple of objects in it. 例如,假设您有一个包含几个对象的数组。 Two of the objects have strong references to each other. 其中两个对象具有很强的相互引用。 Then, the array loses its owner and is destroyed. 然后,阵列失去其所有者并被销毁。 BUT, the two objects in that array that point to each other are NOT destroyed since they have strong references. 但是,该数组中指向彼此的两个对象不会被破坏,因为它们具有强引用。 Thus, you have two objects which you cannot access since the owning array is destroyed, but they're still taking up memory. 因此,你有两个你无法访问的对象,因为拥有的数组被销毁,但它们仍然占用内存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM