) Objective-C diary ② self usage

Source: Internet
Author: User

to be transferred from an Sina Blog, You can watch the qianfeng video to learn about" dog walking principles "first ". This article is well written. It turns around ......

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 this in the official documentation.Code:

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; Memory counter retaincount
Self. Myobject = amyobject; // myobject retaincount = 2;Memory counter retaincount + 1
[Amyobject release]; // myobject retaincount = 1;
Memory counters release: 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;Easy to cause memory leakage

4. Do not add self.

Myobject = [[myobject alloc] init]; // myobject retaincount = 1; The object memory has been released. If called, an exception occurs.

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 property 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;
}

Put it in your class. When you call self. myobject (do not place it on the left of the equal sign, that will call the get method), this method will be called.
Here, I would like to say that @ Property sets an access method for you. It has nothing to do with your properties. You can write only 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, That sentence is just a normal value assignment. Assigning a pointer to another pointer will not affect the allocated memory. Therefore, do not final [amyobject release] In 2; this sentence is the same as 4. I will not talk about it here. let's look at 1 and 3,

When setmyobject: method is called, newvalue is retaken. We must release the original newvalue. Otherwise, the memory will be leaked. 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.

Author: Pepe
Source: http://pepe.cnblogs.com/
The copyright of this article is shared by the author and the blog. You are welcome to repost this article, but you must keep this statement without the author's consent andArticleThe original text connection is clearly displayed on the page. Otherwise, the legal liability is retained.

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.