Some insights on @property and @synthesize in the development of the iphone

Source: Internet
Author: User

My experience is that @property and @synthesize have the following two functions:

1. Role One

@property is the setter and getter method that declares a variable in the header file. H.

@synthesize is the implementation of the setter and getter methods defined in the M file.

2. Effect Two

@property, you can attach a property that defines a variable method when it is declared. such as retain,assign,readonly,nonautomic and so on.

However, one point to explain is to use the variable name directly to assign a value and use the self. Variable assignment. Like what

. h

Objctname* namevarptr;

@property (Nonautomic,retain) objctname* namevarptr;

. m

Self.namevarptr = [[ObjectName alloc] init];

int n = [self.namevarptr retaincount]; n = 2

Namevarptr = [[ObjectName alloc] init];

n = [namevarptr retaincount]; n = 1

Self.namevarptr = [[ObjectName alloc] init] and namevarptr = [[ObjectName alloc] init] What is the difference between the two methods of assignment?

Self.namevarptr=xxx This assignment is equivalent to calling [self setnamevarptr:xxx], and the implementation of the Setnamevarptr:xxx method is dependent on the properties of the @property, such as retain,assign 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

@synthesize understanding of Window=_window and the like

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. In other words, the Window property generates an access method, which is the access method of the _window variable, which is the _window variable.

The following is a common example

@interface myclass:nsobject{

Myobjecct *_myobject;


@property (nonamtic, retain) Myobjecct *myobject;

@implementatin MyClass

@synthesize Myobject=_myobject;


This class declares a variable _myobject, and declares a property called MyObject,

Then @synthesize generates an access method for the attribute MyObject, which should be named Setmyobject and Getmyobject.

The implication of @synthesize Myobject=_myobject is that the access method of the attribute MyObject is the variable _myobject the operation. This usage is common in Apple's sample code,

NOTE: The bitwise operator, on the left side of the equals sign, calls the setter method, and the right side of the equal sign equals the Getter method.

Some insights on @property and @synthesize in iphone development (GO)

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: 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.