iOS @property properties and @synthesize properties (RPM)

Source: Internet
Author: User

When you define a series of variables, you need to write a lot of getter and setter methods, and their form is similar, so Xcode provides the @property and @synthesize properties, @property used in the. h header file as a declaration, @ Synthesize is used in. m files for implementation.

As below, create a project based on "Command line Tool", called "Property", and create a new student class,

The traditional notation is:

Student.h

[CPP]View Plaincopy
  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 and setter method declarations   
  15. -(int) age;
  16. -(void) setage: (int) newage;
  17. //no getter and setter method declarations   
  18. -(int) No;
  19. -(void) Setno: (int) Newno;
  20. @end


Student.m

[CPP]View Plaincopy
  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. implementation of getter and setter method for//age   
  11. -(int) age
  12. {
  13. return  Age ;
  14. }
  15. -(void) setage: (int) newage
  16. {
  17. age = NewAge;
  18. }
  19. implementation of getter and setter method for//no   
  20. -(int) No
  21. {
  22. return  No;
  23. }
  24. -(void) Setno: (int) Newno
  25. {
  26. no = Newno;
  27. }
  28. @end


Main.m

[CPP]View Plaincopy
  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; //This sentence is equivalent to setter method   
  16. NSLog (@"Age is%i", stu.age); //Here the Stu.age equivalent getter method   
  17. [Stu release];
  18. }
  19. return  0;
  20. }


--------------------------------------------------------------------------------------------------------------- ---------with @property and @synthesize are:

Student.h

[CPP]View Plaincopy
  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. //When the compiler encounters @property, it automatically expands into getter and setter declarations   
  15. @property int age ;
  16. @property int No;
  17. @end


Student.m

[CPP]View Plaincopy
  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 will automatically generate getter and setter implementations   
  11. //@synthesize will access the variable with the same name by default, Age,no,height   
  12. //If a variable with the same name is not found, a private variable age,no,height with the same name is automatically generated internally ,   
  13. //So these variables in the Student.h can also be omitted from writing.   
  14. @synthesize Age,no;
  15. @end


Main.m

[CPP]View Plaincopy
  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. }


A few notes:

1. In Xcode4.5 and later versions, you can omit @synthesize, the compiler will automatically help you add getter and setter method implementation, and by default will go to access

_age This member variable, if the _age member variable is not found, a private member variable called _age is automatically generated.

2. Video teaching in the proposed variable name with the "_" prefix as the beginning, but I see big Nerd that book is not used, the individual is more accustomed to the big Nerd that way, so the variable name is not prefixed. Y^o^y

Excerpt from: http://blog.csdn.net/chaoyuan899/article/details/10310719

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.