Attributes and their traits in iOS @property, attribute

Source: Internet
Author: User
Tags first string traits

Property:

Unlike attribute @property and attribute attribute, @property has its own set of processing mechanisms for instance variables in OC. Attribute we can specifically refer to the traits that attributes have or follow.

With attributes, the compiler automatically writes the methods required to access these properties, which are called "Auto-compositing " (autosynthesis). The process of automatic compositing is when the compiler executes at compile time. In addition to this, the compiler also adds an instance variable of the appropriate type to the self-directed class, preceded by an underscore in the attribute name, as the name of the instance variable. You can also specify the name of the instance variable with the @synthesize class:

For example, a first string variable is declared with @property:

@property (Nonatomic,strong) NSString *firstname;

@sybthesize firstName = _myfirstname;//equivalent to changing the name of an instance variable in @property _firstname to _myfirstname

@dynamic Keywords

Tells the compiler not to automatically create an instance variable that implements the property, nor to create an access method for it. Also, when compiling access to the property code, the compiler does not have an error when it discovers that the access method is defined. He believes that these methods can be found during the run time.

For example, if you inherit a subclass from the Nsmanagedobject class in the CoreData framework , you need to dynamically create an access method at run time. Because a property of a subclass is not an instance variable, its data is from the back-end database.

Characteristics of attributes:

@property (nonatomic, ReadWrite, copy) NSString *firstname;// three traits

When you define the access method, you follow the atomicity that corresponds to the attribute trait.

Atomic properties:

Atomic: Ensures the atomicity of its operations through a locking mechanism. If two threads read the same property at the same time, you can always see a valid property value whenever.

Nonatomic: Without locking, when a thread is modifying a property value, another thread may break in and read the property value that has not been modified .

Read and Write privilege traits:

ReadWrite: The property has a Setter,getter method, and if the property is implemented by @synthesize, the compiler automatically generates these two methods.

ReadOnly: The property only has a Get method, and when the property is implemented by the @synthesize, the compiler will obtain a method for its composition, and you can use this trait to expose a property externally as a read-only property, and then redefine it as read-write in. M.

Memory Management semantic Traits:

Assign: Only simple assignment operations on "scalar type " are performed.

Strong: This property defines a "owning relationship " (owning relationship). When you set a new value for this property, the setting method retains the new value, frees the old value, and then sets the new value up.

Weak:"non-owning relationship ". When you set a new value for this property, the setting method does not retain the new value, nor does it release the old value. Property value is also emptied when the object caused by the attribute is destroyed.

unsafe_unretained: similar to assign. But it only applies to "Object type ","non-owning relationship ". When the target object is destroyed, the property value is not automatically emptied (so unsafe,unsafe), which is different from weak.

Copy: The settings method does not retain the new value and copies it . This trait is often used to protect the encapsulation of a property type when it is nsstring*. Because the new value passed to the set method might point to an instance of a sample nsmutablestring class. This class is a subclass of NSString, indicating that it is possible to modify its worthy string. If the string is not copied at this point, the value of the string may be changed without the object's knowledge when the property is set. So this is going to be a copy of the "immutable " (immutable) string. Ensure that the string values in the object are not inadvertently changed.

Method Name Semantic Characteristics:

Getter=<name>: Specifies the method name for the Get method . If the property is Boolean, you can rename the Getter method that gets the property to Ison.

Setter=<name>: generally not.

Some complex properties are initialized with lazy loading:

1. Lazy loading, if lazy loading technique is used, the property must be accessed by accessing the method. Lazy-loaded properties are generally more complex properties. such as custom properties, array properties, dictionary properties, and so on.

2, in the initialization method and Dealloc method, always should be directly through the instance variable to read and write data.

3, when reading data inside the object, it should be read directly through the instance variable, and write the data, should be written by the property.

Attributes and their traits in iOS @property, attribute

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.