First, the traditional get method and set method
In Java, we can access the public member variables of an object by "object name. Member variable name", which is called "point syntax". Before you formally learn the point syntax of OC, take a look at the traditional get and set methods. Defines a student class that has a member variable, age, and the corresponding Get\set method.
Student.h
1 #import <Foundation/Foundation.h>
2
3 @interface Student : NSObject {
4 int age;
5 }
6
7 - (void)setAge:(int)newAge;
8 - (int)age;
9
10 @end
The above code does the following:
(1) In line 4th defines a member variable age, which is @protected permission, so the outside world cannot access it directly
(2) The Set method and get method of the age variable are declared in lines 7th and 8, respectively.
Student.m
1 #import "Student.h"
2
3 @implementation Student
4
5 - (void)setAge:(int)newAge {
6 age = newAge;
7 }
8
9 - (int)age {
10 return age;
11 }
12
13 @end
The above code does the following:
(1) The Set method is implemented on line 5th
(2) The Get method is implemented on line 9th
Main.m
Put the well-defined student class in the main function
#import <Foundation / Foundation.h>
#import "Student.h"
int main (int argc, const char * argv [])
{
@autoreleasepool {
Student * stu = [[Student alloc] init];
// Set the value of age
[stu setAge: 10];
// get the value of age
int age = [stu age];
NSLog (@ "age is% i", age);
[stu release];
}
return 0;
}
The above code does the following:
(1) header file containing student in 2 rows
(2) Create the Student object on line 7th and release the Student object on line 17th
(3) Call the Set method on line 10th to set the value of age
(4) Call the Get method on line 13th to get the value of age
(5) The value of age is output on line 15th and the output is as follows:
2015-12-06 01: 03: 19.002 dot syntax [6164: 303] age is 10
This is a simple use of OC's traditional get method and set method, which is strange for beginners, because its method invocation is done in square brackets []. Thus, OC eventually introduced the dot syntax.
Second, using point syntax instead of the traditional get method and set method
This demonstrates the simple use of the OC traditional Get\set method, followed by the use of point syntax instead.
The code for the main function in the previous MAIN.M can be changed to:
1 int main (int argc, const char * argv [])
2 {
3 @autoreleasepool {
4 Student * stu = [[Student alloc] init];
5
6 // Set the value of age
7 stu.age = 10; // equivalent to [stu setAge: 10];
8
9 // get the value of age
10 int age = stu.age; // equivalent to int age = [stu age];
11
12 NSLog (@ "age is% i", age);
13
14 [stu release];
15}
16 return 0;
17}
1. Note the 7th line of code, replacing the original [Stu Setage:10] with stu.age = 10. Listen clearly, these two kinds of writing are completely equivalent. That is, the stu.age does not represent the member variable age of the Stu object directly, but the compiler automatically expands the code when it encounters stu.age = 10 [stu Setage:10]
Furthermore, if the member variable is accessed directly, the syntax in OC should be: stu->age, not stu.age.
2. Note the 10th line of code, replacing the original int = [Stu Age] with int age = Stu.age. These two formulations are completely equivalent, Stu.age is not directly accessing the member variable age of the Stu object, but the compiler will automatically expand the code into an int when it encounters an int. = Stu.age.
3. Therefore, the meaning of OC midpoint syntax is completely different from that of Java, and the essence of OC Point syntax is method invocation, not directly accessing member variables. As for whether this point syntax represents a get or set method, it depends on whether you are taking a value or setting a value, which is a Get method (such as the 10th Line of code), and the set value is the Set method (such as the 7th line of code).
4. If you want to verify that the point syntax is not a method call, there are many ways. For example, you can use NSLog in the STUDENT.M set method and get method to add some printing information, if the program runs after the output printing information, it is really called the Get method or set method
#import "Student.h"
@implementation Student
-(void) setAge: (int) newAge {
NSLog (@ "setAge method called");
age = newAge;
}
-(int) age {
NSLog (@ "The age method was called");
return age;
}
@end
Third, point grammar and self trap
1 - (void)setAge:(int)newAge {
2 self.age = newAge;
3 }
The self in line 2nd represents the object that is currently calling the Setage: method. But the 2nd line of code is absolutely wrong and will cause a dead loop. As I have said earlier, the essence of OC Point syntax is method invocation, so the above code is equivalent to:
1 - (void)setAge:(int)newAge {
2 [self setAge:newAge];
3 }
Obviously, it causes a loop call to Setage: method, and the program crashes like this.
41 Little tips.
If this is the first contact with OC Point syntax, you might really think that stu.age means directly accessing the member variable age of the Stu object. In fact, part of the reason is that the member variable name of the student Class I define here is called age. To better differentiate between point syntax and member variable access, the member variables we define generally begin with the underscore _. For example, called _age.
1.student.h, note line 4th.
1 #import <Foundation/Foundation.h>
2
3 @interface Student : NSObject {
4 int _age;
5 }
6
7 - (void)setAge:(int)newAge;
8 - (int)age;
9
10 @end
2.STUDENT.M, note lines 6th and 10th.
1 #import "Student.h"
2
3 @implementation Student
4
5 - (void)setAge:(int)newAge {
6 _age = newAge;
7 }
8
9 - (int)age {
10 return _age;
11 }
12
13 @end
"Objective-c Basics-05" keyword Self, dot syntax