The meaning of @synthesize obj=_obj @property and @synthesize

Source: Internet
Author: User

This article reprinted to http://blog.csdn.net/ztp800201/article/details/9231969HTTP://HI.BAIDU.COM/FENG20068123/ITEM/CA8952FA661E5342932AF2C2

Write very good, save a!!!

We often see similar @synthesize Window=_window in the Declarations section of the class when we are developing iOS. The statement, then, what this window is, _ window is what, two things respectively how to use, this is a relatively basic problem, also relates to our understanding of the class, class of the objective-c, class accessor, class of local variables of the unified understanding. 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 @synthesize to protect the variables from inappropriate access.

The following is a common example
Writing one:

@interface myclass:nsobject{

Myobjecct *_myobject;

}

@property (nonamtic, retain) Myobjecct *myobject;

@end

@implementatin MyClass

@synthesize Myobject=_myobject;

Two:

@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 a property myObject access method, 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 statement

Self.namevarptr = [[ObjectName alloc] init]

Namevarptr = [[ObjectName alloc] init]

What is the difference between the two methods 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.

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.

Bad Practice @interface Foo:nsobject {

@private

nsobject* myobj_;

}

@property (Strong, nonatomic) nsobject* myObj;

@end

...

@implementation Foo

@synthesize MYOBJ = myobj_;

    • @end

Good practice.

@interface Foo:nsobject

@property (Strong, nonatomic) nsobject* myObj;

@end

...

@implementation Foo

@synthesize MYOBJ = myobj_;

@end

Still need to declare the underlying name of the variable if you need to access it directly, as when writing custo M getters and Setters:

When you write the setter and getter for the instance variable yourself, if you need to directly access the variable in it, you have to declare the variable with the underlined name:

The wrong wording

@interface Foo:nsobject

@property (Strong, nonatomic) nsobject* myObj;

@end

...

@implementation Foo

@synthesize MYOBJ;

-(nsobject*) MYOBJ

{

return self.myobj; Will call getter! recursively

}

-(void) Setmyobj: (nsobject*) MYOBJ

{

Self.myobj = MYOBJ; Will call setter! recursively

}

@end

The correct wording

@interface Foo:nsobject

@property (Strong, nonatomic) nsobject* myObj;

@end

...

@implementation Foo

@synthesize MYOBJ = myobj_;

-(nsobject*) MYOBJ

{

return myobj_; No problem.

}

-(void) Setmyobj: (nsobject*) MYOBJ

{

No problem

Myobj_ = MYOBJ; To be assigned (arc will handle the necessary retain and release)

}

@end

@property and @synthesize can automatically generate access methods for a class member variable

ReadWrite: This property is the default and will automatically generate accessors for you

Assign: This property is generally used to deal with the underlying type, such as int, float, and so on, if you declare the property is the underlying type, assign is the default, you can not add this property

Natomic: The default is the property, this property is to ensure that the program in multi-threaded situations, the compiler will automatically generate some mutex lock code, to avoid the read and write synchronization of the variable problem

ReadOnly: Only getter Generation will not have setter method

Copy: This automatically generates a clone of your assigned object, which is equivalent to a new copy of the object in memory, so that changing the assignment object will not change the member variable you declared.

Retain: Assignment objects are automatically retain

Nonatomic: If the object does not need to consider multithreading, add this property, which will allow the compiler to generate a few mutually exclusive lock code, can improve efficiency

http://blog.csdn.net/beautifularea/article/details/6886604

Assign: Specifies the setter method with a simple assignment, which is the default action. You can use this property for scalar types, such as int. You can imagine a float, which is not an object, so it can't be retain, copy.

Assign: Simple assignment, without changing the index count (Reference counting). Using assign: for underlying data types (Nsinteger) and C data types (int, float, double, char, etc.)

Retain: Specifies that the retain should be called on a later object, and the previous value sends a release message. You can imagine a nsstring instance, which is an object, and you might want to retain it.

Retain: Releases the old object, assigns the value of the old object to the input object, and then increases the index count of the input object to 1, using retain: For the other nsobject and its subclasses, retain, is to describe the property at the time of assignment, first release the previous value, and then assign the new value to the property, Reference plus 1.

Copy: Specifies that a copy of the object (deep copy) should be used, and the previous value sends a release message. Basically like retain, but without increasing the reference count, it is allocating a new piece of memory to place it. Copy is to create a new object, retain is to create a pointer to reference the object Count plus 1. Copy: Create an object with an index count of 1 and then release the old object, copy is the creation of a new object, retain is a pointer, the reference object Count plus 1.

ReadOnly: will only generate getter methods without generating setter methods (getter methods do not have a get prefix)

ReadWrite: Default property that generates getter and setter methods with no extra parameters (setter method has only one parameter)

Atomic: The default property for an object is that the Setter/getter generated method is an atomic operation. If there are multiple threads calling the setter at the same time, there will not be one thread executing the setter all the way before the other thread starts executing the setter, related to the method's tail and end lock.

Nonatomic: The atomicity of Setter/getter is not guaranteed, and data may be problematic in multithreaded situations. Nonatomic, non-atomic access, no synchronization, multi-threaded concurrent access improves performance. First, the original variable is released, then the new variable is retain and then assigned value;

Note that if you do not add this property, the default is two access methods are atomic transaction access.


Explained very well and studied the

The meaning of @synthesize obj=_obj @property and @synthesize

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.