Objective-C中的@property和@synthesize [英] @property and @synthesize in objective-c

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

问题描述

我在玩耍时,弄清楚了 https://github.com/enormego/EGOTableViewPullRefresh我发现@property和@synthesize神秘.这是我提到的代码

EGORefreshTableHeaderView.h

@interface EGORefreshTableHeaderView : UIView {
    id _delegate;
    EGOPullRefreshState _state;

    UILabel *_lastUpdatedLabel;
    UILabel *_statusLabel;
    CALayer *_arrowImage;
    UIActivityIndicatorView *_activityView;
}

@property(nonatomic,assign) id <EGORefreshTableHeaderDelegate> delegate;

EGORefreshTableHeaderView.m

@synthesize delegate=_delegate;

我已阅读 http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocProperties.html ,据我了解,它为_delegate创建了新的名称,它是委托. (我的理解正确吗?)

但是我仍然不明白为什么他们必须使用@synthesize =指令使事情变得复杂.

解决方案

真的有多复杂?只是一点语法,您就可以指定要用来支持要告诉编译器创建访问器的属性的ivar.如果他们没有提供此名称或等效名称,那么您总是必须让您的属性名称与您的ivar名称相匹配,并且出于某些原因,您可能不希望这样做.

如果您不需要使用不同的名称来命名ivars,那么您就不必指定ivar名称了.实际上,您根本不需要为属性创建ivars,如果不需要,编译器会为您创建.

更新:截至2013年中,LLVM默认为属性的综合访问器,因此在大多数情况下,您不再需要指定@synthesize.您仍将使用它的一种情况是,您要使用与编译器为您生成的实例变量不同的实例变量来备份该属性.同样,支持属性的ivar的默认名称将是带有下划线的属性名称.因此,可以通过删除以下行来简化OP示例中的代码:

id _delegate;

和:

@synthesize delegate=_delegate;

我删除了我先前关于使用下划线前缀的建议,因为它明显不同意编译器的当前方式和默认行为.据我所知,但是,在方法名称上使用下划线前缀仍然是较差的形式.

另外,引起我注意的是,至少有一个人解释了我回答的第一行,这到底有多复杂?"居高临下.我希望这只是一个人的印象-我绝对不打算屈尊取胜,而只是想根据OP关于@synthesize xxx=_xxx;指令使事情变得复杂的主张来构想我的回应.刚开始时,有很多东西需要吸收.希望新的默认情况下合成"行为将减轻新手的负担.

While I was playing and figure out how things work in https://github.com/enormego/EGOTableViewPullRefresh I found mysterious of @property and @synthesize. Here is the code I mentioned

EGORefreshTableHeaderView.h

@interface EGORefreshTableHeaderView : UIView {
    id _delegate;
    EGOPullRefreshState _state;

    UILabel *_lastUpdatedLabel;
    UILabel *_statusLabel;
    CALayer *_arrowImage;
    UIActivityIndicatorView *_activityView;
}

@property(nonatomic,assign) id <EGORefreshTableHeaderDelegate> delegate;

EGORefreshTableHeaderView.m

@synthesize delegate=_delegate;

I have read this http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocProperties.html and from what I understand is it create new name for _delegate which is delegate. (Am I right with this understanding ?)

But I still doesn't understand why they have to make thing complicated with those @synthesize = directive.

解决方案

How complicated is it, really? It's just a bit of syntax that lets you specify the ivar that you want to use to back the property for which you're telling the compiler to create accessors. If they didn't provide this or something equivalent, then you'd always have to have your property names match your ivar names, and there are reasons that you might not want that.

If you don't need to name your ivars differently, then you don't have to bother with specifying the ivar name. In fact, you don't have to create ivars at all for your properties... if you don't, the compiler will create them for you.

Update: As of the middle of 2013, LLVM defaults to synthesizing accessors for properties, so in most cases you no longer need to specify @synthesize at all. The one case where you would still use it is when you want to back the property with a different instance variable than the one that the compiler would generate for you. Also, the default name for the ivar that backs a property will be the property name prefixed with an underscore. So, the code in the OP's example could be simplified by deleting the lines:

id _delegate;

and:

@synthesize delegate=_delegate;

I've removed my previous advice against using an underscore prefix since it clearly disagreed with the current fashion and default behavior of the compiler. As far as I know, it's still poor form to use an underscore prefix for your method names, however.

Also, it has come to my attention that at least one person interpreted the first line of my response, "How complicated is it, really?" as condescending. I hope that was only one person's impression -- I definitely didn't intend any condescension, but was only trying to frame my response around the OP's assertion that the @synthesize xxx=_xxx; directive makes things complicated. There's a lot to absorb when you're starting out; hopefully the new "synthesize by default" behavior will reduce the burden for newcomers.

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

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