在Objective-C,它是最好的做法是:在.h接口或.m文件中的擴展中聲明屬性?
聲明對象如在.H按鈕,然後在.M
.h @interface SomeViewController : UIViewController @property (strong, nonatomic) UIButton *someButton; @end .m @implementation SomeViewController @synthesize someButton = _someButton; @end
合成或它們聲明爲在高德該.M
@interface SomeViewController() @property (strong, nonatomic) UIButton *someButton; @end
我注意到,在很多蘋果代碼,具體兵衛r Breadcrumbs示例代碼,其許多屬性在界面中聲明。兩者有什麼不同?我還注意到,當在@interface
中聲明屬性時,它們會自動合成一個下劃線前綴,使someButton = _someButton
合成無用。
這兩個聲明都是屬性聲明。 ivar由'@ synthesize'創建。它們的功能完全相同;不同的是他們對其他文件的可見性。 –