@synthesize window=_window; statement, then, this window, _ window is what, two things respectively how to use, this is a relatively basic problem, also relates to our understanding objective-c a unified understanding of the class, the properties of the class, the accessors of the class, and the local variables of the class.
In a 32-bit system, if the @interface part of the class does not have a Ivar declaration, but there is a @property declaration that there is a response @synthesize in the @implementation part of the class, you get a compilation error similar to the following:
Synthesized property ' XX ' must either is named the same as a compatible Ivar or must explicitly name an Ivar
At 64-bit, the runtime automatically adds Ivar to the class, adding Ivar prefixed with an underscore "_".
The above declares part of the @synthesize Window=_window; This means that the window property synthesizes accessor methods for _window instance variables.
That is, the window property generation access method is Setwindow, the Setwindow method is the _window variable access method, it operates _window this variable. With this seemingly assignable operation, we can define the names of getters and setters that are not the same as variable names in the @synthesize to protect the variables from inappropriate access.
The following is a common example
Writing one: C code
- @interface myclass:nsobject{
- Myobjecct *_myobject;
- }
- @property (nonamtic, retain) Myobjecct *myobject;
- @end
- @implementatin MyClass
- @synthesize Myobject=_myobject;
Two:C code
- @interface myclass:nsobject{
- }
- @property (nonamtic, retain) Myobjecct *myobject;
- @end
- @implementatin MyClass
- @synthesize Myobject=_myobject;
This class declares a variable _myobject, declares a property called MyObject, and then uses @synthesize to generate the accessor method of the property myObject. The name of this access method should be: Setmyobject and Getmyobject. The implication of @synthesize Myobject=_myobject is that the accessor method of the attribute myObject is used to _myobject the variable. This usage is common in Apple's sample code. after understanding the meaning of this statement, we also know the difference between MyObject and _myobject, then, in the use of the time, there is nothing to pay attention to the place, we should also be clear. Yes,MyObject is a property, and _ MyObject is the variable, and the variables we finally manipulate are myObject. So, the same is the access operation, the statementC code
- Self.namevarptr = [[ObjectName alloc] init]
C code
- Namevarptr = [[ObjectName alloc] init]
What is the difference between the two ways to assign a value?
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.
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.
To sum up, the member variables are assigned to the points to be aware of in order to prevent memory leaks:
1.self how to invoke setter methods
objectname* tmp= [[ObjectName alloc] init];
Self.namevarptr =tmp; retaincount=2
[TMP release]; Retaincount=1
2. Pointer assignment method does not call setter methods
namevarptr= [[ObjectName alloc] init];//Retaincount=1
Therefore, I suggest that everyone in the assignment of a variable to operate, as far as possible to write self.myobj = xxx; That's the surest way.
Statements that are common in iOS development @synthesize obj = _obj in an explanation of meaning