【发布时间】:2013-01-03 02:04:47
【问题描述】:
在 Objective-C 中,最佳实践是:
-
在.h中声明按钮等对象,然后在.m中合成
.h @interface SomeViewController : UIViewController @property (strong, nonatomic) UIButton *someButton; @end .m @implementation SomeViewController @synthesize someButton = _someButton; @end -
或在 .m 中将它们声明为 ivars
@interface SomeViewController () @property (strong, nonatomic) UIButton *someButton; @end
我注意到在很多 Apple 代码中,特别是他们的 Breadcrumbs 示例代码,他们的许多属性都在接口中声明。两者有区别吗?我还注意到,当在@interface 中声明属性时,它们会自动合成带有下划线前缀,从而使someButton = _someButton 合成无用。
【问题讨论】:
-
这两个声明都是属性声明。 ivar 由
@synthesize创建。它们的功能相同;不同之处在于它们对其他文件的可见性。
标签: objective-c coding-style declared-property