When you define a series of variables, you need to write a lot of getter and setter methods, and they are all similar in form, so Xcode provides @property and @synthesize attributes, @property used as a declaration in the. h header file, @ Synthesize is used in. m files for implementation.
For example, create a new project based on Command line Tool, called "Property", and then create a new student class, which is traditionally written as:
Student.h
Copy Code
Student.h
Property
//
Created by rio.king on 13-8-25.
Copyright (c) 2013 rio.king. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Student:nsobject
{
int age;
int no;
}
Age getter and setter method declaration
-(int) age;
-(void) Setage: (int) newage;
Getter and setter method declaration of No
-(int) No;
-(void) Setno: (int) Newno;
@end
Copy Code
Student.m
Copy Code
//
Student.m
Property
//
Created by rio.king on 13-8-25.
Copyright (c) 2013 rio.king. All rights reserved.
//
#import "Student.h"
@implementation Student
Implementation of getter and setter methods for age
-(int) age
{
return age;
}
-(void) Setage: (int) newage
{
age = NewAge;
}
The realization of the getter and setter method of No
-(int) No
{
return no;
}
-(void) Setno: (int) Newno
{
no = Newno;
}
@end
Copy Code
Main.m
Copy Code
//
Main.m
Property
//
Created by rio.king on 13-8-25.
Copyright (c) 2013 rio.king. All rights reserved.
//
Insert code here ...
Student *stu = [[Student alloc] init];
Stu.age = 100;//This sentence is equivalent to setter method
NSLog (@ "Age is%i", stu.age);//The stu.age here is equivalent to the Getter method
Copy Code
//
Student.h
Property
//
Created by rio.king on 13-8-25.
Copyright (c) 2013 rio.king. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Student:nsobject
{
int age;
int no;
}
When the compiler encounters @property, the declaration of getter and setter is automatically expanded
@property int age;
@property int No;
@end
Copy Code
Student.m
Copy Code
//
Student.m
Property
//
Created by rio.king on 13-8-25.
Copyright (c) 2013 rio.king. All rights reserved.
//
#import "Student.h"
@implementation Student
@synthesize will automatically generate getter and setter implementations
@synthesize the default is to access a variable with the same name as Age,no,height,
If a variable of the same name cannot be found, it automatically generates a private variable age,no,height with the same name internally,
Therefore, these variables in the Student.h can also be omitted and not written.
@synthesize Age,no;
@end
Copy Code
Main.m
Copy Code
//
Main.m
Property
//
Created by rio.king on 13-8-25.
Copyright (c) 2013 rio.king. All rights reserved.
//
[Stu release];
}
return 0;
}
Copy Code
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 the default will be to access the _age member variable, if the _age can not find this member variable, will automatically generate a called _ The private member variable of age.
2. Video teaching suggested variable name with "_" prefix as the beginning, but I see big Nerd the book is not used, the individual is also more accustomed to the big Nerd that way, so the variable name is not prefixed.
Introduction to Syntax:
Format: The syntax for declaring the property is: @property (parameter 1, parameter 2) type name; Such as:
C code
@property (Nonatomic,retain) UIWindow *window;
The parameters are mainly divided into three categories:
Read-Write properties: (readwrite/readonly)
Setter semantics: (assign/retain/copy)
Atomicity: (atomicity/nonatomic)
The meaning of each parameter is as follows:
ReadWrite: Generating Setter\getter methods
ReadOnly: Only produce simple getter, no setter.
Assign: Default type, setter method directly assign value without retain action
Retain:setter method to release the old value of the parameter, and then retain the new value.
Copy:setter method for copy operation, same as retain
Nonatomic: Prohibit multiple threads, variable protection, improve performance
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.