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