简体   繁体   English

Objective-C中非合成属性的弱/强注释

[英]Weak/Strong Annotations for Non-Synthesized Properties in Objective-C

Since Objective-C 2.0 we have properties , a nice syntax for getting and setting values for instance variables. 从Objective-C 2.0开始,我们有了一个属性 ,一个很好的语法来获取和设置实例变量的值。 Since Clang 3.1 all properties which are not dynamic, not readonly with an explicit getter or don't have a custom getter and setter are automatically synthesized to ivars. 由于Clang 3.1所有非动态的属性,不具有显式getter的readonly或没有自定义getter和setter,都会自动合成到ivars。 And since ARC we have weak/strong annotations for properties which are used by ARC to define the memory management logic of automatically synthesized properties. 而且,由于ARC,我们对属性使用弱/强注释 ,ARC使用这些属性来定义自动合成属性的内存管理逻辑

The properties still can be synthesized manually eg for a readonly property backed by an ivar and returning a default value, for instance. 例如,属性仍然可以手动合成例如用于由ivar支持的只读属性并返回默认值。

Sometimes, properties are also useful if they are not synthesized at all. 有时,如果它们根本不合成,那么它们也很有用。 I have found a few use cases when I use this sort of behavior: 当我使用这种行为时,我发现了一些用例:

  • A custom getter and setter which use a custom ivar for storing the actual value and which perform some additional actions. 自定义getter和setter,它使用自定义ivar存储实际值并执行一些其他操作。
  • A dynamic property, eg in subclasses of NSManagedObject . 动态属性,例如NSManagedObjectNSManagedObject
  • A readonly property which simply passes through a property of an object stored in another property (eg a private one). 一个只读属性,它只是通过存储在另一个属性(例如私有属性)中的对象的属性。

The Question: Does it makes sense to annotate these non-synthesized properties with weak/strong according to their actual usage or not? 问题:根据实际使用情况,用弱/强注释这些非合成属性是否有意义? What is the best practice? 什么是最佳做法?

( https://twitter.com/kubanekl/status/427142577310408704 ) https://twitter.com/kubanekl/status/427142577310408704

I would say the answer is yes , even if only for documentation sake. 我会说答案是肯定的 ,即使只是为了记录。

Even if you do not use any of the compiler and framework related default implementations, and implement everything by yourself, someone attempting to use these properties will be in much better position of understanding the API if he is able to get a hint on how the memory management would behave. 即使您不使用任何与编译器和框架相关的默认实现,并自己实现所有内容,如果能够获得有关内存的提示,那么尝试使用这些属性的人将更好地理解API。管理会表现出来。 A person does not really have to know how a setter or a getter is implemented internally, but he would might have to know, for example, if after calling a setter, the value was copied or retained or just assigned, and implement his side of things accordingly. 一个人并不真的必须知道如何在内部实现setter或getter,但他可能必须知道,例如,如果在调用setter之后,该值被复制或保留或仅被分配,并实现他的一面相应的事情。

Yes, it does. 是的,它确实。

The property definition is a contract specification. 属性定义是合同规范。 Just because the compiler isn't fulfilling the contract doesn't mean you shouldn't respect it when manually implementing the accessor methods. 仅仅因为编译器没有履行合同并不意味着你在手动实现访问器方法时不应该尊重它。

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

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