iOS自动@synthesize而不创建ivar [英] iOS automatic @synthesize without creating an ivar

查看:97
本文介绍了iOS自动@synthesize而不创建ivar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有 @property 我不想通过 ivar 支持我只是省略了 @synthesize 并且手动获取者返回了一个计算值。

If I have a @property which I didn't want to have backed via an ivar I simply omitted the @synthesize and had manual getters which returned a calculated value.

然而,现在自Xcode 4.4以来我不喜欢指定 @synthesize 编译器会自动生成它。这是否意味着它还会生成 ivar 即使我不需要/使用它?

However, now since Xcode 4.4 if I don't specify @synthesize do compiler will automatically generate it. Does that mean it will also generate an ivar even do I don't need/use it?

我可以最终强制使用 dynamic 进行自动合成。但是这是错误的,因为如果 @dynamic 应该用于关闭警告> getter和setter是在其他地方或运行时实现的。

I could eventually force to not auto-synthesize by using dynamic. However that would be wrong, since @dynamic is supposed to be used for turning off warnings if getter and setter are implemented somewhere else or during runtime.

推荐答案

在我的工作中,我'我注意到以下行为。

In my working with this, I've noticed the following behavior.


  1. 如果你有一个readwrite属性,没有 @synthesize ,有一个getter但没有setter,那么它将生成iVar。

  2. 如果你有一个readwrite属性,那么就没有 @synthesize ,没有getter,并且有一个setter,那么它将生成iVar。

  3. 如果你有一个readwrite属性,没有 @synthesize 并且同时拥有一个getter和setter,那么它将会不生成iVar。

  4. 如果你有一个只读属性,没有 @synthesize 并且没有getter ,然后它将生成iVar。

  5. 如果你有一个只读属性,没有 @synthesize 并有一个getter ,那么它就不会生成iVar。

  1. If you have a readwrite property, don't have a @synthesize, have a getter and don't have a setter, then it will generate the iVar.
  2. If you have a readwrite property, don't have a @synthesize, don't have a getter, and have a setter, then it will generate the iVar.
  3. If you have a readwrite property, don't have a @synthesize and have both a getter and a setter, then it will not generate the iVar.
  4. If you have a readonly property, don't have a @synthesize and don't have a getter, then it will generate the iVar.
  5. If you have a readonly property, don't have a @synthesize and have a getter, then it will not generate the iVar.

从这一点来看,我认为一般规则是如果你没有 @synthesize ,并拥有完全实现该属性所需的所有方法,然后假定它是动态的并且不生成iVar。

From this, I think the general rule is that if you don't have a @synthesize, and have all the methods needed to fully implement the property, then it's assumed to be dynamic and doesn't generate the iVar.

无论如何,如果你想确保没有生成iVar,那么将其声明为 @dynamic

At any rate, if you want to ensure that an iVar is not generated then declare it as @dynamic.

关于@dynamic的澄清

来自声明的属性。 html> Objective-C编程语言:

From Declared Properties in The Objective-C Programming Language:


使用@dynamic关键字告诉编译器您将满足通过直接提供方法实现或在运行时使用其他机制(如动态加载代码或动态方法解析)来隐含属性所隐含的API契约。

You use the @dynamic keyword to tell the compiler that you will fulfill the API contract implied by a property either by providing method implementations directly or at runtime using other mechanisms such as dynamic loading of code or dynamic method resolution.

对我而言,即使您直接实现getter和setter,也可以将属性标记为@dynamic。

To me this reads like it OK to mark a property as @dynamic even when you are directly implementing the getter and setter.

这篇关于iOS自动@synthesize而不创建ivar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆