2012-07-02 19 views
0

可能重複:
Is there any reason to declare ivars if you're using properties exclusively in Objective-C?合成屬性是否需要指針在他們的類接口?

這個類:

@interface MyClass 
{ 
    Something* pointer; 
} 
@property (nonatomic, retain) Something* pointer; 
@end 

@implementation MyClass 
@synthesize pointer 
@end 

顯然運行在相同:

@interface MyClass 
@property (nonatomic, retain) Something* pointer; 
@end 

@implementation MyClass 
@synthesize pointer 
@end 

WHE你創建一個綜合屬性,你還需要一個指向它的類接口的指針嗎?我注意到我的程序顯然運行相同,不管我是否有指針。指針是否被聲明更好?

+0

[有沒有任何理由聲明ivars,如果你只在Objective-C中使用屬性?](http://stackoverflow.com/q/4903651/),[屬性和實例變量](http :[//stackoverflow.com/q/3074248/),[我們應該聲明ivars還是讓編譯器去做?](http://stackoverflow.com/q/8047128/),[Property declaration and automatic backing storage]( http://stackoverflow.com/q/3238009/),[我們需要聲明ivars嗎?](http://stackoverflow.com/q/5864221/) –

+0

可能[其他](http:// stackoverflow。 COM /搜索q =聲明+的ivars +爲+屬性+%5Bobjc%5D&提交=搜索)。 –

回答

1

你曾經,但你不再。編譯器會自動爲你填寫它,所以你只需要聲明@property@synthesize它。

+0

既然哪個Xcode版本? – JoJo

+0

它是現代運行時的一個屬性,它已經在iOS的所有版本以及Mac上的OSX 10.5之後使用。 Documentation [here](https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtVersionsPlatforms.html#//apple_ref/doc/uid/TP40008048-CH106) – Dima

相關問題