Objective-C language keyword Property

Source: Internet
Author: User

@ Property

Introduction

Objective-C language keyword, paired with @ synthesize.

Function: Let the compiler automatically write a method declaration with the same name as the data member to save the declaration of the read/write method.

For example:

1. In the header file:

@ Property int count;

It is equivalent to declaring two methods in the header file:

-(INT) count;

-(Void) setcount :( INT) newcount;

2. Implementation file (. m)

@ Synthesize count;

It is equivalent to implementing two methods in the implementation file (. m.

-(INT) count

{

Return count;

}

-(Void) setcount :( INT) newcount

{

Count = newcount;

}

The above equivalent functions are automatically filled by the compiler to help developers, simplifying the coding input workload.

Format

The syntax for declaring property is:

@ Property (parameter 1, parameter 2) type name;

For example:

@ Property (nonatomic, retain) uiwindow * window;

There are three types of parameters:

Read/write attributes: (readwrite/readonly)

Setter semantics: (assign/retain/copy)

Atomicity: (Atomicity/nonatomic)

The parameter meanings are as follows: readwrite

Generate the setter \ getter Method

Readonly

Only generate simple getter without setter.

Assign

Default type. The setter method directly assigns values without retain operations.

Retain

The setter method performs the release old value on the parameter, and then retain the new value.

Copy

The setter method performs the copy operation, which is the same as the retain method.

Nonatomic

Multithreading and Variable Protection are prohibited to improve performance.

Parameter types

The retain and copy parameters are complex. The specific analysis is as follows:

Getter Analysis

1. @ property (nonatomic, retain) test * thetest; @ property (nonatomic, copy) test * thetest;

Equivalent code:

-(Void) thetest

{

Return thetest;

}

2. @ property (retain) test * thetest;

@ Property (copy) test * thetest;

Equivalent code:

-(Void) thetest

{

[Thetest retain];

Return [thetest autorelease];

}

Setter Analysis

1,

@ Property (nonatomic, retain) test * thetest;

@ Property (retain) test * thetest;

It is equivalent:

-(Void) setthetest :( test *) newthetest {

If (thetest! = Newthetest ){

[Thetestrelease];

Thetest = [newthetest retain];

}

}

2. @ property (nonatomic, copy) test * thetest;

@ Property (copy) test * thetest;

It is equivalent:

-(Void) setthetest :( test *) newthetest {

If (thetest! = Newthetest ){

[Thetestrelease];

Thetest = [newthetest copy];

}

}

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.