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

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

问题描述

我对目标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;

vs.

- (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: just方法.他们没有什么神奇的.它们不是特殊的属性方法".它们只是按照约定访问状态的方法.该状态通常存储在一个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.something不是 的意思是从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天全站免登陆