Objective-C attributes

Source: Internet
Author: User

We know that in objective-C, using @ property with @ synthesize enables the compiler to automatically implement the getter/setter method, which is convenient to use and can be directly used.Object. Attribute.

Nsstring * Name;
Nsuinteger age;

@ Property (nonatomic, copy) nsstring*Name;
@ Property (assign) nsuinteger age;

@ Synthesize name;

@ Synthesize age;  


If we wantObject. MethodTo call a method and obtain the return value of the method, you need to use @ property with @ dynamic.

@ Property ( Readonly ) Nsstring * Firstarrayvalue;

@ Dynamic firstarrayvalue;

-(Nsstring*) Firstarrayvalue
{
Return[_ Array objectatindex:0];

}


In this way, the first value in the _ array can be obtained using the object. firstarrayvalue. Obviously, this method is not applicable to the method that requires parameter passing.

 

In fact, the @ dynamic keyword is used to tell the compiler to implement the access method by ourselves.

If @ synthesize is used, the working compiler will help you implement it.

 

Note:CodeThe sample code is used only. In actual use, each sentence of code should be placed in the corresponding position.

 

===== the last reposted about @ property (*) attribute content in parentheses ====

Readonly

This tag indicates that the attribute is read-only, and the default tag is read/write. If you specify read-only @ Implementation Only one reader is required. Or if you use @ Synthesize Keyword. The reader method is also parsed. And if you try to assign values to attributes using the vertex operator, you will get a compilation error.

 

Readwrite

This tag indicates that the attribute is read/write, which is also the default attribute. Both the configurator and reader must be in @ Implementation . If you use @ Synthesize Keyword. Both the reader and the setter are parsed.

 

Assign

This flag indicates that the setter directly assigns a value, which is also the default value. Applications that use garbage collectionProgramIf you want an attribute to use Assign And this class conforms Nscopying Protocol, you must specify this tag rather than simply using the default value. Otherwise, you will get a compilation warning. This again shows to the compiler that you really need to assign values, even if it is copyable.

 

Retain

Specify Retain Will wake up the incoming value when the value is assigned Retain Message. This attribute can only be used Objective-C Object type, but cannot be used Core Foundation Object. ( The reason is obvious, Retain Will increase the reference count of the object, and the basic data type or Core Foundation No reference count for objects -- Translator's note ) .

 

copy

It indicates that a copy of the input value is used when the value is assigned. Copy work Copy Method execution. This attribute only applies Nscopying The object type of the Protocol is valid. For more information, see " Copy " .

 

Nonatomic

It indicates that accessors are not atomic operations, but by default, accessors are atomic operations. That is to say, in a multi-threaded environment, the parser provides a secure access to the attributes. The returned values obtained from the reader or the values set by the setter can be completed at one time, even other threads are accessing it. If you do not specify Nonatomic In the self-managed memory environment, the resolved accessors retain and automatically release the returned values. Nonatomic The accessor simply returns this value.

 

 

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.