目标C实例变量/属性的多个声明 [英] Objective C multiple declarations of instance variables / properties

查看:68
本文介绍了目标C实例变量/属性的多个声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对ObjC还是很陌生.我注意到创建您的

I'm still pretty new to ObjC. I noticed that it's pretty standard everywhere to create your

@interface myObj : NSObject {
    id delegate;
    NSDictionary *dict;
}

然后

@property (nonatomic,retain) NSDictionary *dict;
@property (retain) id delegate;

-例如.我知道通过

--for example. I know how useful the auto code generation + clearer definition of @property is thanks to the Declared Properties page over at Apple. What I do not understand, however, is why it's standard for people to do both -- declare their properties and then have them again in the {curly brackets}.

我的意思是,如果我有一个班级想要一些变量具有自动获取器/设置器,而有些则没有,那么我将理解为常规变量使用{}块,然后仅创建@ property/@仅针对那些我想要具有附加功能的特定变量进行综合语句;但是为什么在您希望所有实例var都具有getter和setter的情况下始终同时拥有这两个标准,这又为什么呢?我想跳出这个行列是因为当我觉得确实没有必要时,基本上可以看到它在100%的时间内都用过了……只需声明@properties并保留它即可.

I mean, if I had a class where I wanted some of the variables to have auto getters/setters and some not to, then I would understand having the {} block for my regular vars and then only creating @property/@synthesize statements for just those specific variables I wanted to have the added functionality; but why is it standard to always have both in cases where you know that you want all of your instance vars to have the getters and setters? I guess I'm tripping out because I'm basically seeing it used like this 100% of the time when I feel like it really isn't necessary... just declare the @properties and leave it at that.

有什么想法吗?最佳编码实践建议?还是这里我缺少一些信息?

Thoughts? Best coding practice suggestions? Or is there some information I'm missing here?

推荐答案

Objective-C的早期版本中需要您看到的内容,但现在不再需要.

What you're seeing was required in earlier versions of Objective-C, but isn't any more.

在NeXT使用的Objective-C的第一个版本中,直到引入新的运行时为止(在Mac OS X上使用Objective-C 2.0),所有实例变量都必须在其.原因是,如果您对一个类进行子类化,则编译器需要知道该类的实例变量布局,以便它可以看到将子类的实例变量放置在什么偏移量上.

In the first versions of Objective-C used by NeXT up until the new runtime was introduced (with Objective-C 2.0 on Mac OS X), all instance variables had to be declared as part of the class's structure in its @interface. The reason was that if you subclassed a class, the compiler needed to know the instance variable layout of the class so it could see at what offset to put the subclass's instance variables.

引入属性后,综合属性必须由类结构中的实例变量支持".因此,您必须同时声明一个实例变量和该属性.

When properties were introduced, synthesized properties had to be "backed" by an instance variable in the class's structure. Therefore you had to declare both an instance variable and the property.

以上所有内容均不再适用.较新的Objective-C查找实例变量偏移量的方式不那么脆弱,这意味着需要进行一些更改:

All of the above is no longer true. Newer Objective-C is less fragile in the way it looks up instance variable offsets, which has meant a few changes:

  • 并非所有实例变量都必须位于@interface中.现在可以在@implementation中进行定义:由于冲突和其他问题的可能性,因此无法在类别中进行定义.
  • 可以基于属性定义来推断和创建用于综合属性的实例变量.
  • 您可以以编程方式将实例变量添加到正在运行时创建的类中(仅在将类注册为系统可用之前).
  • not all instance variables need to be in the @interface. They can now be defined in the @implementation: though not in categories due to the possibilities of clashing and other issues.
  • instance variables for synthesized properties can be inferred and created based on the property definition.
  • you can programmatically add instance variables to classes you're creating at runtime (only before you've registered the class as available to the system).

因此,重申一下,您只需要在Objective-C语言的旧版本中声明实例变量和综合属性.您看到的是多余的,不应被视为最佳实践".

So, to reiterate, you only needed to declare both the instance variable and a synthesized property in older versions of the Objective-C language. What you're seeing is redundant and should not be considered a "best practice".

这篇关于目标C实例变量/属性的多个声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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