Objective-c Summary of self usage

Source: Internet
Author: User

Address: http://www.cocoachina.com/macdev/objc/2012/0613/4355.html

When to use self. most of the replies in the online search or forum are simply said that this is related to the objc access method, few people have answered the question about the relationship between them and their access methods. The following code describes the problem:

Create a Student class that inherits the NSObject class. Code:

#import <Foundation/Foundation.h>@ interface Student : NSObject{    NSString *_idd;    NSString *_name;}@property (nonatomic, retain) NSString *idd;@property (nonatomic, retain) NSString *name;@end

. M file code:

#import "Student.h"@implementation Student@synthesize idd = _idd;@synthesize name = _name;  - (void)dealloc{    [_idd release];    _idd = nil;    [_name release];    _name = nil;    [super dealloc];}@end

 

Use @ propety @ synthesize to implement the set get method of the member attribute of Student. Generally, we access the attributes of Student members in other classes:

Get the student name through student. name: assign a value to the name [student setName: @ "jordy"]. student is a Student class object. If you access its member attributes within the Student class, use [self setName: @ "jordy"], access using self. name;

Note: Due to wordpress, the characters in the Code are automatically saved in Chinese format. Remember to change the format to English when using it.

In Student. h and Student. m files are our habitual writing method, but it still does not seem to be able to explain the difference between adding self and not adding self. Please refer to the following code, which is another habitual writing method, take the Student class as an example:

. H file code:

#import <UIKit/UIKit.h>@ class Student;  @ interface ViewController : UIViewController{    Student *_student;}@property (nonatomic, retain) Student *student;@end

. 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];}

It can be noted that the above Code, with the previous code, is changed to the _ name variable in the. h file; the @ sythesize Writing Style in the. m file has also changed.

If the self. _ name: Get the attribute value. The xcode compiler will prompt an error. In fact, this shows that we usually use self. name actually uses the get method of the student class name, and the same is true for the set Method of name.

The following describes the differences between using self and not using self from memory management:

ViewController. h file, using the Student class, the Code is as follows:

#import <UIKit/UIKit.h>@ 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];

This is something I believe someone may have a question about. If it is so complicated to create a student object, it seems that self is directly used. student = [[Student alloc] init]; is there no problem either. It is quite normal to not add self?

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 indicates the reference count of the object. student's property is retain's default reference count + 1 using self. student.

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.

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.