ios的@property屬性和@synthesize屬性

  當你定義了一系列的變量時,需要寫很多的getter和setter方法,而且它們的形式都是差不多的,,所以Xcode提供了@property和@synthesize屬性,@property用在 .h 頭文件中用作聲明,@synthesize用在.m 文件中用於實現。

如下,新建一個基於“Command Line Tool”的項目,名爲“property”,再新建一個Student類,

傳統的寫法是:

Student.h

  1. //  
  2. //  Student.h  
  3. //  property  
  4. //  
  5. //  Created by Rio.King on 13-8-25.  
  6. //  Copyright (c) 2013年 Rio.King. All rights reserved.  
  7. //  
  8.   
  9. #import <Foundation/Foundation.h>  
  10.   
  11. @interface Student : NSObject  
  12. {  
  13.     int age;  
  14.     int no;  
  15. }  
  16.   
  17. //age的getter和setter方法聲明  
  18. - (int)age;  
  19. - (void)setAge:(int)newAge;  
  20.   
  21. //no的getter和setter方法聲明  
  22. - (int)no;  
  23. - (void)setNo:(int)newNo;  
  24.   
  25. @end  

Student.m

  1. //  
  2. //  Student.m  
  3. //  property  
  4. //  
  5. //  Created by Rio.King on 13-8-25.  
  6. //  Copyright (c) 2013年 Rio.King. All rights reserved.  
  7. //  
  8.   
  9. #import "Student.h"  
  10.   
  11. @implementation Student  
  12.   
  13. //age的getter和setter方法的實現  
  14. - (int)age  
  15. {  
  16.     return age;  
  17. }  
  18. -(void)setAge:(int)newAge  
  19. {  
  20.     age = newAge;  
  21. }  
  22.   
  23. //no的getter和setter方法的實現  
  24. - (int)no  
  25. {  
  26.     return no;  
  27. }  
  28. - (void)setNo:(int)newNo  
  29. {  
  30.     no = newNo;  
  31. }  
  32.   
  33. @end  

main.m

  1. //  
  2. //  main.m  
  3. //  property  
  4. //  
  5. //  Created by Rio.King on 13-8-25.  
  6. //  Copyright (c) 2013年 Rio.King. All rights reserved.  
  7. //  
  8.   
  9. #import <Foundation/Foundation.h>  
  10. #import "Student.h"   
  11.   
  12. int main(int argc, const char * argv[])  
  13. {  
  14.   
  15.     @autoreleasepool {  
  16.           
  17.         // insert code here...  
  18.         Student *stu = [[Student alloc] init];  
  19.         stu.age = 100;//這句相當於setter方法  
  20.         NSLog(@"age is %i", stu.age);//這裏的 stu.age 相當於getter方法  
  21.           
  22.         [stu release];  
  23.           
  24.     }  
  25.     return 0;  
  26. }  

------------------------------------------------------------------------------------------------------------------------

用@property和@synthesize的寫法是:

 Student.h

  1. //  
  2. //  Student.h  
  3. //  property  
  4. //  
  5. //  Created by Rio.King on 13-8-25.  
  6. //  Copyright (c) 2013年 Rio.King. All rights reserved.  
  7. //  
  8.   
  9. #import <Foundation/Foundation.h>  
  10.   
  11. @interface Student : NSObject  
  12. {  
  13.     int age;  
  14.     int no;  
  15. }  
  16.   
  17. //當編譯器遇到@property時,會自動展開成getter和setter的聲明  
  18. @property int age;  
  19. @property int no;  
  20.   
  21.   
  22. @end  

Student.m

  1. //  
  2. //  Student.m  
  3. //  property  
  4. //  
  5. //  Created by Rio.King on 13-8-25.  
  6. //  Copyright (c) 2013年 Rio.King. All rights reserved.  
  7. //  
  8.   
  9. #import "Student.h"  
  10.   
  11. @implementation Student  
  12.   
  13. //@synthesize 會自動生成getter和setter的實現  
  14. //@synthesize 默認會去訪問age,no,height同名的變量,,  
  15. //如果找不到同名的變量,會在內部自動生成一個私有同名變量age,no,height,,  
  16. //因此Student.h 中的這幾個變量也可以省略不寫。  
  17. @synthesize age,no;  
  18.   
  19. @end  

main.m

  1. //  
  2. //  main.m  
  3. //  property  
  4. //  
  5. //  Created by Rio.King on 13-8-25.  
  6. //  Copyright (c) 2013年 Rio.King. All rights reserved.  
  7. //  
  8.   
  9. #import <Foundation/Foundation.h>  
  10. #import "Student.h"  
  11.   
  12. int main(int argc, const char * argv[])  
  13. {  
  14.       
  15.     @autoreleasepool {  
  16.           
  17.         // insert code here...  
  18.         Student *stu = [[Student alloc] init];  
  19.         stu.age = 100;  
  20.         NSLog(@"age is %i", stu.age);  
  21.           
  22.         [stu release];  
  23.     }  
  24.     return 0;  
  25. }  

幾點說明:

1.在Xcode4.5及以後的版本中,可以省略@synthesize ,編譯器會自動幫你加上getter 和 setter 方法的實現,並且默認會去訪問

_age這個成員變量,如果找不到_age這個成員變量,會自動生成一個叫做 _age私有成員變量。

2.視頻教學中建議變量名用"_"前綴作爲開頭,但我看big Nerd 那本書裏是不用的,個人也比較習慣 big Nerd 的那種寫法,所以變量名就不加前綴了。Y^o^Y 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章