Characteristics of @property

Source: Internet
Author: User

@property also have some keywords that have special effects, such as Nonatomic,strong in the above code:

12 @property(nonatomic,strong) NSString *carName;@property(nonatomic,strong) NSString *carType;

I divide them into three categories: atomicity , accessor control , memory management .

4.1 atomicity

Atomic(default): Atomic means that the operation is atomic, meaning that only one thread accesses the instance variable. The atomic is thread-safe, at least in the current accessor. It is a default feature, but is rarely used because the comparison affects efficiency, which is related to the arm platform and the internal locking mechanism.

nonatomic: Nonatomic is just the opposite of atomic. Represents a non-atomic and can be accessed by multiple threads. Its efficiency is faster than atomic. However, there is no guarantee of security in a multithreaded environment, which is widely used in the case of single-threaded and explicitly only one thread access.

4.2 Accessor control

readwrite(default): ReadWrite is the default value, indicating that the property has both a setter and a getter.

readonly: ReadOnly says only getter has no setter.

Sometimes the name of a custom accessor may be required to be more specific to the semantics:

1 @property (nonatomic, setter = mySetter:,getter = myGetter ) NSString *name;

The most common is the bool type, such as the attribute hidden that identifies whether the view is hidden. It is possible to declare:

1 @property (nonatomic,getter = isHidden ) BOOL hidden;

4.3 Memory management

@property have a memory management policy displayed. This allows us to take a look at the @property declaration to see how it treats incoming values.

assign(default): Assign is used for value types, such as int, float, double, and nsinteger,cgfloat, which represent pure replication. It also includes objects that do not have ownership relationships, such as common delegate.

1 @property(nonatomic) int running;
1 @property(nonatomic,assign) int running;

The above two pieces of code are the same.

In setter methods, the set value operation is implemented by direct assignment:

123 -(void)setRunning:(int)newRunning{      _running = newRunning;  }

Retian: In the Setter method, a reference count of the incoming object needs to be added to the 1 operation.

In a nutshell, ownership of an incoming object will not be released as long as it has ownership of the object. As shown in the following code:

12345678 -(void)setName:(NSString*)_name{       //首先判断是否与旧对象一致,如果不一致进行赋值。       //因为如果是一个对象的话,进行if内的代码会造成一个极端的情况:当此name的retain为1时,使此次的set操作让实例name提前释放,而达不到赋值目的。       if( name != _name){            [name release];            name = [_name retain];       }  }

Strong: Strong is the keyword introduced when iOS introduces Arc and is an optional alternative to retain. Indicates that the instance variable has a ownership relationship to the incoming object, which is a strong reference. Strong and retain have the same meaning and produce the same code, but the semantics better reflect the relationship of the object.

weak: In the Setter method, the incoming object needs no reference count plus 1 operation.

Simply put, there is no ownership of the incoming object, when the object reference count is 0 o'clock, that is, after the object is disposed, the instance variable declared with weak points to nil, that is, the value of the instance variable is 0.

Note: The weak keyword was introduced by IOS5, and IOS5 was not used before the keyword. Delegate and Outlet are generally declared by weak.

copy: Similar to strong, but the difference is that the instance variable has ownership of the copy of the incoming object, not the object itself.

From:http://www.cocoachina.com/ios/20150123/11003.html

Characteristics of @property

Related Article

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.