@property 原理

@property 原理

 1 @interface Demo : NSObject

 2 {

 3     NSString *name;

 4 }

 5 

 6 @property (nonatomic, copy) NSString *name;

 7 @end

 8 

 9 @implementation Demo

10 @synthesize name;

11 

12 - (void)print

13 {

14     NSLog(@"name : %@", self.name);

15 }

16 @end

@property 是由编译器在编译时刻为其生成getter and setter方法。其下为setter实现:

 1 @implementation

 2 - (void)setName:(NSString *)aName

 3 {

 4     [aName retain];

 5     [iName release];

 6     iName = aName;

 7 }

 8 

 9 - (instancetype)getName

10 {

11     return [iName autorelease];

12 }

13 @end

 

你可能感兴趣的:(property)