iOS的@property和@synthesize屬性

分類:IT技術 時間:2016-10-19

當你定義了一系列的變量時,需要寫很多的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. #import <Foundation/Foundation.h>
  9. @interface Student : NSObject
  10. {
  11. int age;
  12. int no;
  13. }
  14. //age的getter和setter方法聲明
  15. - (int)age;
  16. - (void)setAge:(int)newAge;
  17. //no的getter和setter方法聲明
  18. - (int)no;
  19. - (void)setNo:(int)newNo;
  20. @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. #import "Student.h"
  9. @implementation Student
  10. //age的getter和setter方法的實現
  11. - (int)age
  12. {
  13. return age;
  14. }
  15. -(void)setAge:(int)newAge
  16. {
  17. age = newAge;
  18. }
  19. //no的getter和setter方法的實現
  20. - (int)no
  21. {
  22. return no;
  23. }
  24. - (void)setNo:(int)newNo
  25. {
  26. no = newNo;
  27. }
  28. @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. #import <Foundation/Foundation.h>
  9. #import "Student.h"
  10. int main(int argc, const char * argv[])
  11. {
  12. @autoreleasepool {
  13. // insert code here...
  14. Student *stu = [[Student alloc] init];
  15. stu.age = 100;//這句相當於setter方法
  16. NSLog(@"age is %i", stu.age);//這裏的 stu.age 相當於getter方法
  17. [stu release];
  18. }
  19. return 0;
  20. }




用@property和@synthesize的寫法是:

Student.h

[cpp] view plain copy print ?
  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. #import <Foundation/Foundation.h>
  9. @interface Student : NSObject
  10. {
  11. int age;
  12. int no;
  13. }
  14. //當編譯器遇到@property時,會自動展開成getter和setter的聲明
  15. @property int age;
  16. @property int no;
  17. @end





Student.m [cpp] view plain copy print ?
  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. #import "Student.h"
  9. @implementation Student
  10. //@synthesize 會自動生成getter和setter的實現
  11. //@synthesize 默認會去訪問age,no,height同名的變量,,
  12. //如果找不到同名的變量,會在內部自動生成一個私有同名變量age,no,height,,
  13. //因此Student.h 中的這幾個變量也可以省略不寫。
  14. @synthesize age,no;
  15. @end








main.m [cpp] view plain copy print ?
  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. #import <Foundation/Foundation.h>
  9. #import "Student.h"
  10. int main(int argc, const char * argv[])
  11. {
  12. @autoreleasepool {
  13. // insert code here...
  14. Student *stu = [[Student alloc] init];
  15. stu.age = 100;
  16. NSLog(@"age is %i", stu.age);
  17. [stu release];
  18. }
  19. return 0;
  20. }


幾點說明:

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

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

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









Tags: Copyright interface property import 而且

文章來源:


ads
ads

相關文章
ads

相關文章

ad