IOS development self. Usage Summary

Source: Internet
Author: User

The following are some of my summary. If something is wrong, I hope you will give me more advice.

We can see this code in the official document:

In the MyClass. h file

@ Interface MyClass: NSObject {MyObject * myObject ;}
@ Property (nonatomic, retain) MyObject * myObject;
@ End

In the MyClass. m file

@ Synthesize myObject;
-(Id) init {
If (self = [super init]) {
MyObject * aMyObject = [[MyObject alloc] init];
Self. myObject = aMyObject;
[AMyObject release];
}
Return self;
}

Why should we use the above assignment method? Don't self. myObject = [[MyObject alloc] init]; or myObject = [[MyObject alloc] init]; No? Don't upload objects by callback. Here are some of my opinions.


First: When self. ..................................

MyObject * aMyObject = [[MyObject alloc] init]; // aMyObject retainCount = 1;
Self. myObject = aMyObject; // myObject retainCount = 2;
[AMyObject release]; // myObject retainCount = 1;

Second: When self. is not added
MyObject * aMyObject = [[MyObject alloc] init]; // aMyObject retainCount = 1;
MyObject = aMyObject; // myObject retainCount = 1;
[AMyObject release]; // The myObject is released;

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.