Objective-C接口-声明变量vs只是属性? [英] objective-c interface - declaring variable vs just property?

查看:63
本文介绍了Objective-C接口-声明变量vs只是属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Obj-c中,在@interface内声明变量时

In Obj-c when declaring a variable within @interface

@interface:NSObject { MyObject * myObject}

@interface: NSObject{ MyObject* myObject}

@属性(不安全,非原子)MyObject * myObject;

@property (unsafe, nonatomic) MyObject* myObject;

VS.仅将其声明为属性

Vs. Only declare it as a property

@interface:NSObject {}

@interface: NSObject{}

@属性(不安全,非原子)MyObject * myObject; @end

@property (unsafe, nonatomic) MyObject* myObject; @end

在这里没有声明任何变量吗?

Not declare any var here?

问候 基督徒

推荐答案

@property定义接口,而不是实现.在您的情况下,您要定义一个readwrite属性.这意味着您有望实现-myObject-setMyObject:.这与ivars无关.

@property defines an interface, not an implementation. In your case, you're defining a readwrite property. This means that you're promising to implement -myObject and -setMyObject:. This has nothing to do with ivars.

现在,实现这些方法的最常见方法是让它们由一个ivar支持.为方便起见,ObjC允许您使用@synthesize myObject=myObject_;使用ivar存储区自动生成所需的方法.这表示使用自动创建的名为myObject_的ivar为属性myObject创建所需的方法". ivar myObject_是真正的ivar,您可以正常访问它(尽管通常不应该访问;应该使用访问器).

Now, the most common way to implement those methods is by having them be backed by an ivar. As a convenience, ObjC lets you automatically generate the required methods with an ivar store using @synthesize myObject=myObject_; This says "create the required methods for the property myObject using an automatically created ivar called myObject_." The ivar myObject_ is a real ivar, and you can access it normally (though you generally shouldn't; you should use accessors).

您可以只实现-myObject-setMyObject:,而不是使用@synthesize.您甚至可以使用@dynamic myObject;告诉编译器不必担心此属性的实现;它将在运行时正确处理."

Instead of using @synthesize, you could just implement -myObject and -setMyObject:. You could even use @dynamic myObject; to tell the compiler "don't worry about the implementations for this property; it'll be handled correctly at runtime."

@property与仅声明方法之间有一些区别,但原则上此行:

There are a few differences between @property and just declaring methods, but in principle, this line:

@property (nonatomic, readwrite, strong) MyObject* myObject;

在概念上与此相同:

- (MyObject *)myObject;
- (void)setMyObject:(MyObject *)anObject;

在这里声明自己属于ivar并没有真正的影响.您仍然需要以某种方式实现这些方法.如果您命名的ivar与@synthesize使用的ivar相同,则@synthesize不会创建新的ivar.

Declaring the ivar yourself has no real impact here. You still need to implement the methods somehow. If your named ivar is the same as the ivar @synthesize is using, then @synthesize just won't create a new ivar.

作为一种实践,我不再鼓励人们声明ivars.我建议仅将公有和私有属性与@synthesize一起使用,以创建任何所需的ivars.如果由于某种原因必须拥有手动ivar,则建议在@implementation块而不是@interface中声明它们.

As a matter of practice, I discourage people from declaring ivars anymore. I recommend just using public and private properties with @synthesize to create any needed ivars. If you must have a manual ivar for some reason, then I recommend declaring them in the @implementation block rather than the @interface.

这篇关于Objective-C接口-声明变量vs只是属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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