directory of this document
- Objective
- First, the traditional get method and set method
- Second, using point syntax instead of the traditional get method and set method
- Third, point grammar and self trap
- 41 Little tips.
Description: This objective-c topic is a prelude to learning iOS development, and to enable programmers with experience in object-oriented language development to quickly get started with objective-c. If you do not have programming experience, or are not interested in objective-c, iOS development, please ignore. Before studying this topic, it is recommended to study the C language topic first.
Back to the top of the preface
In Java, we can access the public member variables of an object by "object name. Member variable name", which is called "point syntax". Like what:
1. The 2nd line of the Student class defines a public member variable, age
1 public class Student {
2 public int age;
3 }
2. Then, in line 5th, the member variable age of Stu is assigned directly by point syntax
1 public class Test {
2
3 public static void main(String[] args) {
4 Student stu = new Student();
5 stu.age = 10;
6 }
7
8 }
Of course, the formal approach is to privatize member variables, allowing the outside world to access member variables using public get methods and set methods.
3. Many high-level languages have this point syntax, in order to allow programmers in other industries to quickly get started Oc,oc also introduced the point syntax, but its meaning is not the same as Java
Back to top one, the traditional get method and set method
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.
1.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
1> defines a member variable age in line 4th, which is @protected permission, so the outside world cannot access it directly
2> the Set method and the Get method for the age variable in line 7th and 8, respectively
2.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
1> implements the Set method on line 5th
2> implemented the Get method on line 9th
3.main.m
Put the well-defined student class in the main function
1 #import <Foundation / Foundation.h>
2 #import "Student.h"
3
4 int main (int argc, const char * argv [])
5 {
6 @autoreleasepool {
7 Student * stu = [[Student alloc] init];
8
9 // Set the value of age
10 [stu setAge: 10];
11
12 // Get the value of age
13 int age = [stu age];
14
15 NSLog (@ "age is% i", age);
16
17 [stu release];
18}
19 return 0;
20}
1> header file containing student in line 2
2> create student object on line 7th, release 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> outputs the value of age on line 15th, and the output is as follows:
2013-04-08 00:26:19.002 point 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.
Back to top second, use 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
1 #import "Student.h"
2
3 @implementation Student
4
5-(void) setAge: (int) newAge {
6 NSLog (@ "setAge method called");
7 age = newAge;
8 }
9
10-(int) age {
11 NSLog (@ "The age method was called");
12 return age;
13}
14
15 @end
Go back to the top three, dot grammar and self trap
1. In Java, the This keyword represents the method caller, that is, who called the method and this represents who. So the set method is generally written like this:
1 public void setAge(int newAge) {
2 this.age = newAge;
3 }
Line 2nd assigns the value of the NewAge parameter to the member variable of the method caller, age
There is a self keyword in 2.OC that behaves like this keyword. When I say this, someone might want to write the OC's Set method:
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.
Back to top 41 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" 06-point syntax