不需要在界面中放置属性吗? [英] Don't need to put properties inside the interface?

查看:79
本文介绍了不需要在界面中放置属性吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为我在Objective-C方面有一本不好的书.它说将要使用的变量放在接口括号内,并提供给其他类使用,然后声明相同内容的属性.

I think I got a bad book on Objective-C. It said to put variables I'm going to use and make available to other classes inside the interface brackets, and then declare a property of the same thing.

@interface appDelegate : NSObject <UIApplicationDelegate>  {
    NSNumber *myNumberOtherClassesCanUse;
}
@property (nonatomic, retain) NSNumber *myNumberOtherClassesCanUse;

但是,我看到另一个问题的答案使我觉得我不必将它们放在方括号内,因此我尝试了一下,但仍然可以正常工作.

However, I saw another question answered that made me think I didn't have to put them inside the brackets, so I tried that and it still works fine.

我买了另一本书,也没有解释,所以有人可以告诉我或指出我什么时候必须将它们放在方括号中并使其具有属性吗?

I bought another book and it doesn't explain it either, so can someone tell me or point me to documentation on when I have to put them inside the brackets AND make them properties?

推荐答案

一个漫长而复杂的答案:

A long and complicated answer:

类字段,界面括号内的内容:

The class fields, the things inside the interface brackets:

@interface MyClass : SomeSuperclass
{
  int someNumber_; /* this stuff */
}

将实际字节添加到您的对象.实际上,Objective-C类实际上是一个大型C结构;这些是该结构的字段.(您甚至可以使用-> 样式C指针语法来获取它们,尽管如果您在其中没有 @public 的情况下编译器会对您大喊大叫;像其他变量一样,使用裸名来访问这些字段.)

add actual bytes to your object. An objective-C class is really, underneath all the fluff, just a large C struct; and these are the fields of that struct. (You can even get at them with -> style C pointer syntax, although the compiler will yell at you if you do so without an @public in there; the correct way to access these fields is by bare name like any other variable.)

属性声明,例如:

@property (nonatomic, assign, readwrite) int someNumber;

是obj-C 2.0的缩写,它是一对方法声明,例如:

is obj-C 2.0 shorthand for a pair of method declarations like:

-(int)someNumber;
-(void)setSomeNumber:(int)newNumber;

这不会为您的课程添加任何内容;它只是向世界宣告了可以在此类上执行的一些操作.按照惯例,这样的方法应该获取并设置一个字段值,或者至少表现得像它们一样.具有这样的方法可以让您使用 obj.prop obj.prop = val 点语法来调用它们.

which adds nothing to your class; it just declares to the world that here are some operations that can be performed on this class. By convention, methods like this are supposed to get and set a field value, or at least behave like they do. Having methods like this lets you use the obj.prop and obj.prop = val dot-syntax to call them.

synthesize指令:

A synthesize directive:

@synthesize someNumber;

(可选)使用标准的内置行为(由该非原子的,分配"的内容告知)在运行时为您创建这些方法, AND (可选)创建一个具有相同名称的字段以进行获取和设置.像 @synthesize someNumber = someNumber _; 这样的指令将创建的字段命名为"someNumber_".如果像我在上面那样在@interface块中声明了这样的字段,它将使用该字段.否则,它会悄悄地插入自己的适当字段.自从4.x仿真器以来,这种安静的字段插入是新行为.在此之前,您必须在@interface {}块,句点中声明所有字段.

optionally creates these methods for you at runtime, using standard builtin behaviors (informed by that "nonatomic, assign" stuff), AND optionally creates a field of the same name to get and set. A directive like @synthesize someNumber = someNumber_; names the created field "someNumber_" instead. If you declared such a field in the @interface block, like I did above, it uses that field; otherwise it quietly inserts an appropriate field of its own. This quiet field insertion is new behavior since the 4.x simulator; before that you had to declare all your fields in the @interface{} block, period.

总结:@interface {...}描述了对象的物理字段;@property()...描述了应该使用对象的方式;@synthesize是实现基本getter/setter并在需要时插入额外字段的简写.通常,我让@synthesize来做它的事情,并将我的@interface {}块留空,直到我真正需要显式声明一个字段为止,通常是用调试器查看它.

In summary: @interface{...} describes the physical fields of your object; @property()... describes ways your object is supposed to be used; and @synthesize is shorthand for implementing basic getter/setters and inserting extra fields if needed. I usually let @synthesize do its thing, and leave my @interface{} block empty until I have an actual need to declare a field explicitly, usually to look at it with the debugger.

[从最新的SDK开始,以上某些内容已过时.特别是,@synthesize现在是可选的,实例变量可以在.m文件的 @implementation 中声明.]

[as of the latest SDKs, some of the above is dated. In particular, @synthesize is now optional, and instance vars can be declared in the @implementation in the .m file.]

这篇关于不需要在界面中放置属性吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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