Statements common in iOS development @synthesize obj = _obj explanation of meaning

Source: Internet
Author: User

@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
    1. @interface myclass:nsobject{
    2. Myobjecct *_myobject;
    3. }
    4. @property (nonamtic, retain) Myobjecct *myobject;
    5. @end
    6. @implementatin MyClass
    7. @synthesize Myobject=_myobject;

Two:C code
    1. @interface myclass:nsobject{
    2. }
    3. @property (nonamtic, retain) Myobjecct *myobject;
    4. @end
    5. @implementatin MyClass
    6. @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
    1. Self.namevarptr = [[ObjectName alloc] init]
C code
    1. 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

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.