当我在玩游戏并弄清楚在https://github.com/enormego/egotableviewpullrefresh中的事情是如何工作的,我发现了@property和@synthesize的神秘。这是我提到的代码
@interface EGORefreshTableHeaderView : UIView {
id _delegate;
EGOPullRefreshState _state;
UILabel *_lastUpdatedLabel;
UILabel *_statusLabel;
CALayer *_arrowImage;
UIActivityIndicatorView *_activityView;
}
@property(nonatomic,assign) id <EGORefreshTableHeaderDelegate> delegate;
@synthesize delegate=_delegate;
我已经阅读了此http://developer.apple.com/library/mac/#documentation/cocoa/cocoa/conceptual/objectual/obigntivec/chapters/ocproperties/ocproperties.html,,我了解是为_delegate创建_delegate的新名称。 (我对这种理解是对的吗?) 但我仍然不明白为什么他们必须使这些事情与@synthesize = Directive的事情变得复杂。
真的很复杂吗?这只是一些语法,可以让您指定要使用的IVAR来支持您要告诉编译器创建访问者的属性。如果他们没有提供此或等效的内容,那么您始终必须让您的属性名称与您的ivar名称匹配,并且有一些理由您可能不想要它。
update:
从2013年中期开始,LLVM默认为合成属性的访问器,因此在大多数情况下,您根本不需要指定@synthesize
来简化OP示例中的代码:
id _delegate;
@synthesize delegate=_delegate;
我已经删除了我以前的建议,反对使用强调前缀,因为它显然不同意编译器的当前时尚和默认行为。据我所知,对于您的方法名称,使用下划线前缀仍然很差。
您是对的,使用
@synthesize foobar=_foobar;
@synthesize foobar=fluffybunny;
每次使用登录器
fluffybunny
在复杂性方面,您是否宁愿写
.foobar
or
@synthesize
the并不是特别写得特别好,因为我忘记了如何做得好,但是@Synthesize指令阻止了您不得不写很多次访问者。这是对OBJ-C 1.0的大量吸引的一件事。
无代码,不要敲打它。