@property 和 @synthesize [英] @property and @synthesize

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

问题描述

我对 Objective C 很陌生.(两天了).当读到 @synthesize 时,它似乎与我的理解 @property 重叠(我以为我理解了)......所以,一些细节需要在我的介意......它困扰着我.

I'm very new to Objective C. (Two days now). When read about @synthesize, it seemed to overlap with my understanding @property (which I thought I understood) ... So, some details need to be ironed out in my mind ... it's bugging me.

如果我对@property@synthesize 的区别有误,请纠正我:

Please correct me if I'm wrong about differences of @property and @synthesize:

如果您在 @interface 中声明了一个 @property,那么您就是在告诉全世界用户可以期望为该属性使用标准的 getter 和 setter.此外,XCode 将为您制作通用的 getter 和 setter. ...但是,@property 声明在多大程度上发生了?(即,这是否意味着完全"......就像在你的 @interface 中看不见的声明,以及在你的 @interface 中看不见的代码?

If you declare a @property in your @interface, then you're telling the world that users can expect to use standard getters and setters for that property. Futhermore, XCode will make generic getters and setters for you. ... BUT, To what degree does that happen with the @property declaration? ( I.E. does that mean "completely" ... like unseen declarations for it in your @interface, and also unseen code in your @interface?

-或-

@property 是否只处理 @interface 中看不见的代码声明 - 而 @synthesize 处理看不见的代码实现在您的 @implementation 部分?)

Does @property take care of the unseen code declarations in your @interface only - whereas @synthesize takes care of the unseen code implementation in your @implementation section? )

推荐答案

首先,请注意最新版本的 Xcode 根本不再需要 @synthesize.您可以(并且应该)忽略它.也就是说,这就是这些部件的作用.

First, note that the latest version of Xcode does not require @synthesize at all anymore. You can (and should) just omit it. That said, here's what the pieces do.

@property 是访问器的声明.这只是一个声明.以下内容几乎没有区别:

@property is a declaration of accessors. It is just a declaration. There is very little difference between the following:

@property (nonatomic, readwrite, strong) NSString *something;

对比

- (NSString *)something;
- (void)setSomething:(NSString)aSomething;

主要区别在于使用 @property 声明这些方法可以让编译器自动为您生成(综合)实现.没有要求您让编译器为您做这件事.您可以完全自由地手动实现 somethingsetSomething:,这很常见.但是,如果您不手动实现它们,编译器会自动为您创建一个名为 _something 的 ivar,并为 getter 和 setter 创建一个合理的实现.

The main difference is that declaring these methods using @property lets the compiler automatically generate (synthesize) the implementations for you. There is no requirement that you let the compiler do it for you. You are absolutely free to implement something and setSomething: by hand, and it is common to do. But, if you don't implement them by hand, the compiler will automatically create an ivar for you called _something and create a reasonable implementation for the getter and setter.

在旧版本的 Xcode 中,您必须使用 @synthesize 关键字明确请求自动生成.但这不再是必需的.今天,使用 @synthesize 的唯一原因是如果您希望 ivar 具有非标准名称(永远不要那样做).

In older versions of Xcode, you had to explicitly request the auto-generation using the @synthesize keyword. But that is no longer required. Today, the only reason to use @synthesize is if you want the ivar to have a non-standard name (never do that).

这里的一个关键点是somethingsetSomething: 方法只是方法.他们没有什么神奇之处.它们不是特殊的属性方法".它们只是按照惯例访问一个状态的方法.那段状态通常存储在 ivar 中,但并不需要如此.

A key point here is that the methods something and setSomething: are just methods. There is nothing magical about them. They're not special "property methods." They're just methods that by convention access a piece of state. That piece of state is often stored in an ivar, but does not need to be.

更清楚一点:object.somethingnot 的意思是从 object 返回名为 _something 的 ivar>."它的意思是返回 [object something] 的结果,不管它做什么."返回 ivar 的值是很常见的.

To be even more clear: object.something does not mean "return the ivar named _something from object." It means "return the result of [object something], whatever that does." It is common for that to return the value of an ivar.

您应该使用 @property 声明来声明您的所有状态(内部和外部),并且您应该避免直接声明 ivars.除了在 initdealloc 方法中,您还应该始终通过它们的访问器(self.something)访问您的属性.在initdealloc中,你应该直接使用ivar(_something).

You should declare all of your state (internal and external) using @property declarations, and you should avoid directly declaring ivars. You should also always access your properties via their accessors (self.something), except in the init and dealloc methods. In init and dealloc, you should directly use the ivar (_something).

这篇关于@property 和 @synthesize的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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