Most of the answers are: "This is related to the objc access method"
How can this problem be solved? Next, let's take a few small examples.
First, create a Student class: Student Class.
This student class contains the student id and the Student name.
- # Import
-
-
- @ Interface
- Student: NSObject {
-
- // Idname
-
- NSString * id;
-
- NSString * name;
- }
-
- @ Property
- (Nonatomic, strong) NSString * id;
- @ Property
- (Nonatomic, strong) NSString * name;
-
- @ End
-
- Student implementation file
-
- # Import
- "Student. h"
-
- @ Implementation
- Student
-
- @ Synthesize
- Id, name;
-
- @ End
If you use the above method to define the get and set methods for the attributes of the student class, the access to other classes will be:
Use student. name to obtain the name of student. If you assign a value to the name, use [student
SetName: @ "eve"]; student is the object of the Student class. If you access its member attributes within the Student class, use [self
SetName: @ "evo"], access using self. name;
The above method is only one, but it is difficult to explain whether self should be used. See the following:
We rewrite the Student class.
- # Import
-
-
- @ Interface
- Student: NSObject {
-
- // Idname
-
- NSString * _ id;
-
- NSString * _ name;
- }
-
- @ Property
- (Nonatomic, strong) NSString * id;
- @ Property
- (Nonatomic, strong) NSString * name;
-
- @ End
-
- . M file
-
- # Import
- "Student. h"
-
- @ Implementation
- Student
-
- @ Synthesize
- Id = _ id;
- @ Synthesize
- Name = _ name;
-
- @ End
It can be seen that we have added _ id and _ name for this writing method, and @ synthesize has also changed.
When we use the self. name compiler at this time, an error will be reported. This shows that we usually use the get method of the student class name, and the set Method of name is the same.
In addition, some people also explain from the memory management aspect on the network. I cut it out for learning:
ViewController. h file, using the Student class, the Code is as follows:
- # Import
- @
- Class Student;
-
- @
- Interface ViewController: UIViewController {
-
- Student * _ student;
- }
-
- @ Property
- (Nonatomic, retain) Student * student;
-
- @ End
-
- ViewController. m file, code:
-
- # Import
- "ViewController. h"
- # Import
- "Student. h"
-
- @ Implementation
- ViewController
- @ Synthesize
- Student = _ student;
-
- -
- (Void) didReceiveMemoryWarning
- {
-
- [Super didReceiveMemoryWarning];
- }
-
- # Pragma
- Mark-View lifecycle
-
- -
- (Void) viewDidLoad
- {
-
- [Super viewDidLoad];
- }
-
- -
- (Void) dealloc
- {
-
- [_ Student release];
-
- _ Student = nil;
-
- [Super dealloc];
- }
- Other methods are not used, so they are not displayed here.
-
- Create a Student class object in the viewDidLoad method of ViewController. m.
-
- Student
- * Mystudent = [[Student alloc] init];
- Self. student
- = Mystudent;
- [Mystudent
- Release];
Next we need to analyze the differences between them from the memory perspective:
1. Add self:
- Student
- * Mystudent = [[Student alloc] init]; // mystudent object
- RetainCount = 1;
- Self. student
- = Mystudent; // student object retainCount = 2;
- [Mystudent
- Release]; // student object retainCount = 1;
- RetainCount refers to the object reference count, the property of student
- Yes, retain uses self. student reference count + 1 by default.
2. Do not add self
- Student
- * Mystudent = [[Student alloc] init]; // mystudent object
- RetainCount = 1;
- Student
- = Mystudent; // student object retainCount = 1;
- [Mystudent
- Release]; // the memory of the student object has been released. If it is called, an exception occurs.
3. Add self direct assignment
Self. student = [[Student alloc] init]; // student object retainCount =
2. Easy to cause memory leakage
Since objective-c memory management is processed based on the reference count, gcc releases the memory when the reference count of an object is zero.
Personal Summary: you only need to use the self. Attribute during attribute initialization, and directly use the attribute name in other cases. Using self. enables retaincount + 1. To ensure that the current class has ownership of this attribute
Personal habits:
- @ Interface CustomClass: UIViewController
- {
- NSString * str
- }
- @ Property (retain, nonatomic) NSString * str; @ implementation CustomClass @ synthesize str;-void) viewDidLoad
- {// Method 1 alloc must be manually released once self. str = [[NSString alloc] initWithString: @ "my str"];
- [Str release]; // method 2 using class methods without self. str = [NSString stringWithString: @ "my str"];
-
- Use str directly in future calls without using self. str
- [Str appendString: @ "\ n"];
- } // Dealloc must be released-(void) dealloc
- {// Method 1 [str release];
- Str = nil; // method 2 self. str = nil;
-
- [Super dealloc];
- }