Object-c @ property usage

Source: Internet
Author: User

Property is a code generation mechanism that can generate different types of getter/setter functions, especially if you want to use vertex (.) to access a variable, you must use the property.

 

How to use it?

Usage example: @ property (attribute1, attribute2) float value;

 

These attributes include:

Readonly-Read-only, read-only, but not set value (the setxxxx function cannot be used ).

Readwrite-Readable and writable (default ).

Assign-Replace the old and new variables (default) when setting values ).

Retain-When the value is set, the new variable is retain, and the old variable is release.

Copy-Copy a new variable when setting the value, and release the old variable.

Nonatomic-The default value is atomic.

Strong-In the reference counting environment, strong is assumed to be the same as retain;

Weak was introduced from the 5.0 system. Its function is similar to assign, but it is automatically set to nil when the referenced object is 0.

The first two are just simple settings of variable readability.

 

Assign is just a simple replacement of variables, usually used in scalar types, such as nsinterger and cgrect,

Or (in the reference counting environment) for objects you do not own, such as delegates.

In the garbage collection environment, retain and assign are actually the same.

The content of the setter code generated by it is similar:


-(Void) setvalue :( float) newvalue {

Value = newvalue;

}

 

Another important thing to note is that when using property, you must add self (such as self. XXX) to the front. If you do not do this, easy causes memory leakage.

Object-c @ property usage

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.