2010-09-06 33 views
1

@property and @synthesize有什麼用?你能用一個例子來解釋一下嗎?Objective-C中使用的@property和@synthesize是什麼?

+1

幫助自己,讀一本書或蘋果的目標C介紹:http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Introduction/introObjectiveC.html – 2011-10-11 11:00:15

回答

3

apple developer library

你可以把屬性聲明爲等同於聲明兩個存取方法。因此

@property float value; 

等同於:

- (float)value; 

- (void)setValue:(float)newValue; 

,並通過使用@synthesize,編譯器創建存取方法,你(看更多here

1
// Sample for @property and @sythesize // 

@interface ClassA 

    NSString *str; 

@end 

@implementation ClassA 

@end 

The Main Function main() 

//確保你#import ClassA

ClassA *obj=[[ClassA alloc]init]; 

[email protected]"XYZ"; // That time it will give the error that we don't have the getter or setter method. To use string like this we use @property and @sythesize 
相關問題