How to Use Object-c @ property

Source: Internet
Author: User

How to Use Object-c @ property

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, the default value is strong, which works the same as retain;

Weak was introduced from System 5.0, which is similar to assign. However, when the referenced object is 0, it is automatically set to nil.

The first two are just simple settings of variable readability.

Assign is a simple replacement variable, which is 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, it is easy to cause memory leakage.

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.