property setting and meaning summary for iOS variables

Source: Internet
Author: User

A recent opportunity to get a comprehensive overview of the properties of iOS:

IOS @property and @synthesize help us easily generate object getter and setter methods to accomplish the assignment and access of objects. But if we want to animate the getter and setter methods of an object, you can use the @property and @dynamic combinations. Object access Methods property has many properties set, such as: Atomic and Nonatomic,readwrite and Readonly,retain,assign and Copy,strong and weak.

1.atomic and Nonatomic

Atomic is the default property, which means that the operation of the object is atomic, mainly in multi-threaded environments, providing multi-threaded access security. We know that access to objects under multiple threads requires lock-in access before unlocking, guaranteeing that there will not be several operations on the same object at the same time. If the programming does not involve multi-threading, it is not recommended, because using atomic is more expensive than nonatomic system resources.

Nonatomic indicates that access to accessors is not atomic and does not support multithreaded access security, but has high access performance.

2.readwrite and ReadOnly

ReadWrite is the default property, which means that objects can be read and written, and the corresponding setter and getter methods are generated for the object.

ReadOnly means that only the value of the object is allowed to be read, and only the getter method of the object is generated.

3.retain,assign and copy

Retain represents the release of the old value for NSObject and its subclass objects, and then retain the new value, increasing the application count of the object by one.

This property can only be used with the Obejective-c type object, not the core Foundation object. (Retain increases the reference count of the object, and the base data type or core foundation object has no reference count, and the reference count increases by 1 when the object is added to the array).

-(void) Setoldvalue: (nsstring*) newvalue {

if (newvalue!=oldvalue) {

[OldValue release];

OldValue = [NewValue retain];

}

}

Assign is the default property and can only be used for basic data types such as Cgfloat,nsinteger,bool,int, proxy objects, and so on. This method assigns a value directly to the object without retain operation.

Copy means re-establishing a new object with a count of 1, and then releasing the old value.

All know that retain is a copy of the pointer, copy is a copy of the content. For example: The address of the NSString object is 0x100, the content is "string", if you use Copy to another NSString object, it will generate another object with the address of 0x110, but the content is still ' string '. If you use retain to another NSString object, the address of the object is still 0x100, except that the count of the object becomes 2.

4.strong and weak

In Arc mode, object declarations need to include strong and weak to facilitate automatic memory management. The strong type is the default.

Strong strong reference, the default property, similar to retain, is actually a relative concept, which is a reference. If a strong reference holds the object, the object cannot be freed. All of the default instance and local variables are strong pointers.

Weak weak references, similar to assign, are the same as strong references, except that they do not determine the survival of the object. Even if an object is held by countless references, as long as there is no strong reference to him, then it will be purged, it is not the owner of the object. Its value is automatically set to nil after the object is disposed.

The weak pointer is used primarily for "parent-child" relationships, where the father has a strong pointer to the son, so the father is the owner of the son, but in order to prevent the cycle of ownership, the son needs to use the weak pointer pointing to his father. A typical example is the delegate mode, where your viewcontroller has a uitableview through the strong pointer (Self.view), UITableView DataSource and delegate are weak pointers, Point to your viewcontroller.

property setting and meaning summary for iOS variables

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.