IOS development (31) Summary of self. Usage

Source: Internet
Author: User

Recently, someone asked me when to use self. assignment. I summarized it and sent it for your reference. Please make an axe if you have any questions.

  

When self .. is used is actually related to the access method of Obj-c, but many people have answered this question on the Internet. Why is it related to the access method? Why? Not many people have answered this question. For details about memory management, please refer to the Objective-C Memory Management Summary of trademanager ~ For CC Special Edition, I will not explain much about some things.

We often see the following code in the official documents:

MyClass. h

@ Interface MyClass: NSObject {

MyObject * myObject;

}

@ Property (nonatomic, retain) MyObject * myObject;

@ End

  

MyClass. m

@ Synthesize myObject;

  

-(Id) init

{

If (self = [super init])

{

MyObject * aMyObject = [[MyObject alloc] init];

Self. myObject = aMyObject;

[AMyObject release];

}

Return self;

}

Some people ask why such a complicated assignment is required? Why should I add self .? Directly write self. myObject = [[MyObject alloc] init]; isn't it true? Sometimes it seems that it is normal to not add self?

Now let's take a look at the memory management content:

  

First look at the indirect assignment:

1. Add self.

MyObject * aMyObject = [[MyObject alloc] init]; // aMyObject retainCount = 1;

Self. myObject = aMyObject; // myObject retainCount = 2;

[AMyObject release]; // myObject retainCount = 1;

  

2. Do not add self.

MyObject * aMyObject = [[MyObject alloc] init]; // aMyObject retainCount = 1;

MyObject = aMyObject; // myObject retainCount = 1;

[AMyObject release]; // the object has been released

  

Let's look at the directly assigned values:

3. Add self.

Self. myObject = [[MyObject alloc] init]; // myObject retainCount = 2;

  

4. Do not add self.

MyObject = [[MyObject alloc] init]; // myObject retainCount = 1;

  

Is it a bit dizzy now, let's change the code first, a common official method:

  

MyClass. h

@ Interface MyClass: NSObject {

MyObject * _ myObject;

}

@ Property (nonatomic, retain) MyObject * myObject;

@ End

  

MyClass. m

@ Synthesize myObject = _ myObject;

  

OK. Try again now. If you use self. _ myObject = aMyObject; or myObject = aMyObject; you will get an error. Why? This is related to the Obj-c access method. to put it simply, we all know that @ property (nonatomic, retain) MyObject * myObject; is used to set an access method for an attribute, but the method name and attribute name we use are the same at ordinary times, now you have written it into different names, and it will be clear. _ myObject is the instance variable and myObject is the name of the access method.

  

Now we know that self is the access method for access attributes. How does the access method work? Self. myObject = [[MyObject alloc] init]; why is memory leakage?

I will not explain nonatomic much about it. It is not the focus of my talk, and I have not fully figured it out and won't mislead everyone. I only talk about assign, retain, copy.

The get method is:

-(MyObject *) myObject {

Return _ myObject;

}

  

The set method is:

// Assign

-(Void) setMyObject :( id) newValue

{

_ MyObject = newValue;

}

// Retain

-(Void) setMyObject :( id) newValue

{

If (_ myObject! = NewValue)

{

[_ MyObject release];

_ MyObject = [newValue retain];

}

}

// Copy

-(Void) setMyObject :( id) newValue

{

If (_ myObject! = NewValue)

{

[_ MyObject release];

_ MyObject = [newValue copy];

}

}

  

In fact, there are other content in these methods, not just these. And these methods can be rewritten. For example, if you write

-(MyObject *) myObject

{

Return _ myObject;

}

In your class, you call self. myObject (do not place it on the left side of the equal sign, it will call the setter method, # add doesn't matter, it is also called by the getter method first) will call this method.

  

Here, I would like to say that @ property sets an access method for you and has nothing to do with your attributes (# add is an instance variable). You can just write one sentence.

@ Property (readonly) NSString * name;

  

Implement in your class

-(NSString *) name

{

NSLog (@ "name ");

Return @ "MyClass ";

}

You can also use self. name to call.

  

Now let's look back at the four assignments we started,

When self. is not used, the sentence is just a normal value assignment. Assigning a pointer to another pointer will not affect the allocated memory,

Therefore, do not end with [aMyObject release] In 2; this sentence is the same as 4. I will not talk about it here.

Let's take a look at 1 and 3. When setMyObject: method is called, we perform a retain operation on newValue. We must release the original newValue, otherwise the memory will leak, in 1, we have a aMyObject that can be released. In 3, we cannot release it. Therefore, in 3, we will add a retainCount. memory leakage.

  

After talking about this, I just want to let everyone know what is the call property itself and what is the call access method. how can we avoid Memory leakage? In the above example, it is called in your own class. If this class is called by another class, pay more attention to it,

  

By the way, if you want to access object attributes in other classes, instead of using the access method, you can use myClass-> myObject to access the object directly, however, you must first set myObject to @ public. but this is not officially promoted,

  

The code is relatively simple. I still sent it out, so high people can ignore it.

 

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.