Self. And the use of _object

Source: Internet
Author: User

I remember learning iOS a long time ago when I was using arc and self directly. method to define the variable, at that time for the Set,get method is also around, not how to dig, recently to do an MRC project maintenance and upgrade, have to re-learn the memory management of the content. Of course, there is an article in the set method. We will slowly find that these two are important in our future programming, and we often need to go back in order to be able to do some of the work we want to do.

The following describes the use of the set method need to pay attention to the problem, we directly define a variable set method, is to do the overload, in which can not only assign values, but also to perform the operation of the leaf update, is not very good? This has been overlooked in the past. It is important to note that _object, we know sometimes self. And _object can read the variable, but when overloaded, the difference can be reflected.

1. To make the overloaded method execute, use self. To get this variable

2. Inside the set method, you cannot use self. Otherwise it will cause circular call, is not very around, do a test yourself can be very straightforward to understand

3. It is important to note that when you assign a value to a variable, if _objct gets it, we know that the set will not be called and therefore will not be retain, and will crash in the final release, such as:

UILabel * label == label; [ Label release];

This is a simple assignment without any problems, but if we release the variable MyLabel in the Dealloc, we will get an error,

-(void  ) dealloc{   [_mylabel release];       [Super release];} It's wrong to use it together with the above .

At this point, we have two ways to avoid this fault,

1) can use Self.mylabel = label, will call the set method, inside again retain operation,

2) do not release the temporary variable label.

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

When we judge a variable in the if (some conditions)

If the condition is a BOOL type variable will be compiled to 0, 1

If the condition is an enumerated variable, it will also be compiled into 0, 1, 2, 3 ... this method to determine whether the enumeration variable exists is incorrect, because the first value is 0, which raises an error judgment

Self. And the use of _object

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.