When to use self for iOS.

Source: Internet
Author: User

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.

 
 
  1. # Import
  2.  
  3.  
  4. @ Interface
  5. Student: NSObject {
  6.  
  7. // Idname
  8.  
  9. NSString * id;
  10.  
  11. NSString * name;
  12. }
  13.  
  14. @ Property
  15. (Nonatomic, strong) NSString * id;
  16. @ Property
  17. (Nonatomic, strong) NSString * name;
  18.  
  19. @ End
  20.  
  21. Student implementation file
  22.  
  23. # Import
  24. "Student. h"
  25.  
  26. @ Implementation
  27. Student
  28.  
  29. @ Synthesize
  30. Id, name;
  31.  
  32. @ 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.

 
 
  1. # Import
  2.  
  3.  
  4. @ Interface
  5. Student: NSObject {
  6.  
  7. // Idname
  8.  
  9. NSString * _ id;
  10.  
  11. NSString * _ name;
  12. }
  13.  
  14. @ Property
  15. (Nonatomic, strong) NSString * id;
  16. @ Property
  17. (Nonatomic, strong) NSString * name;
  18.  
  19. @ End
  20.  
  21. . M file
  22.  
  23. # Import
  24. "Student. h"
  25.  
  26. @ Implementation
  27. Student
  28.  
  29. @ Synthesize
  30. Id = _ id;
  31. @ Synthesize
  32. Name = _ name;
  33.  
  34. @ 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:

 
 
  1. # Import
  2. @
  3. Class Student;
  4.  
  5. @
  6. Interface ViewController: UIViewController {
  7.  
  8. Student * _ student;
  9. }
  10.  
  11. @ Property
  12. (Nonatomic, retain) Student * student;
  13.  
  14. @ End
  15.  
  16. ViewController. m file, code:
  17.  
  18. # Import
  19. "ViewController. h"
  20. # Import
  21. "Student. h"
  22.  
  23. @ Implementation
  24. ViewController
  25. @ Synthesize
  26. Student = _ student;
  27.  
  28. -
  29. (Void) didReceiveMemoryWarning
  30. {
  31.  
  32. [Super didReceiveMemoryWarning];
  33. }
  34.  
  35. # Pragma
  36. Mark-View lifecycle
  37.  
  38. -
  39. (Void) viewDidLoad
  40. {
  41.  
  42. [Super viewDidLoad];
  43. }
  44.  
  45. -
  46. (Void) dealloc
  47. {
  48.  
  49. [_ Student release];
  50.  
  51. _ Student = nil;
  52.  
  53. [Super dealloc];
  54. }
  55. Other methods are not used, so they are not displayed here.
  56.  
  57. Create a Student class object in the viewDidLoad method of ViewController. m.
  58.  
  59. Student
  60. * Mystudent = [[Student alloc] init];
  61. Self. student
  62. = Mystudent;
  63. [Mystudent
  64. Release];

Next we need to analyze the differences between them from the memory perspective:

1. Add self:

 
 
  1. Student
  2. * Mystudent = [[Student alloc] init]; // mystudent object
  3. RetainCount = 1;
  4. Self. student
  5. = Mystudent; // student object retainCount = 2;
  6. [Mystudent
  7. Release]; // student object retainCount = 1;
  8. RetainCount refers to the object reference count, the property of student
  9. Yes, retain uses self. student reference count + 1 by default.

2. Do not add self

 
 
  1. Student
  2. * Mystudent = [[Student alloc] init]; // mystudent object
  3. RetainCount = 1;
  4. Student
  5. = Mystudent; // student object retainCount = 1;
  6. [Mystudent
  7. 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:

 
 
  1. @ Interface CustomClass: UIViewController
  2. {
  3. NSString * str
  4. }
  5. @ Property (retain, nonatomic) NSString * str; @ implementation CustomClass @ synthesize str;-void) viewDidLoad
  6. {// Method 1 alloc must be manually released once self. str = [[NSString alloc] initWithString: @ "my str"];
  7. [Str release]; // method 2 using class methods without self. str = [NSString stringWithString: @ "my str"];
  8.  
  9. Use str directly in future calls without using self. str
  10. [Str appendString: @ "\ n"];
  11. } // Dealloc must be released-(void) dealloc
  12. {// Method 1 [str release];
  13. Str = nil; // method 2 self. str = nil;
  14.  
  15. [Super dealloc];
  16. }

Related Article

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.