Object-c declaring attributes why underline

Source: Internet
Author: User

see a lot of source code, use the preceding underlined variable, and then use a variable name that is not underlined in the @synthesize statement. What is the effect of doing this? Baidu later found that the use of the underlined variable just an instance variable, such as @synthesize name = _name We can also use Foo instead of _name, just a code of the specification and programming style, the underscore is only to facilitate the difference between global variables and local variables, or prevent variable names. usually in the header file definition of @property variables, must be @synthesize.  
Self.namevarptr = [[ObjectName alloc] init]   

Namevarptr = [[ObjectName alloc] init]  

We have seen the above 2 methods, what is the difference between the two kinds of assignment?

Self.namevarptr=xxx This assignment is equivalent to invoking [self setnamevarptr:xxx], and the implementation of the Setnamevarptr:xxx method is dependent on the properties of the @property, such as retain, Assign and other properties. When we declare xxx, we use the property modifier retain. When we use self.xxx, we use the Setxxx method that the compiler generates for us. The Retaincount in this method will be added 1.

Namevarptr = the assignment of XXX, only assigns a pointer to a value. Namevarptr is just a pointer variable that records the address of XXX. In this process, the setter method is not called, and the setter method is not called, it has no relation to the @property, and thus, is not related to attributes such as retain,assign. This method of assignment is a simple pointer assignment.

So let's make a distinction between the 2 methods of assignment:

Self Call setter method objectname*  tmp= =tmp;                 // retaincount=2 [TMP release];                               // retaincount=1

pointers are assigned in a way that does not call setter methods Namevarptr // retaincount=1

When assigning an operation to a variable, try to write self.myobj = xxx; That's the surest way.

Object-c declaring attributes why underline

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.