OC-attributes, point syntax

Source: Internet
Author: User

In OC, the method of assigning a value to a single instance variable is called a setter method (set)

In OC, reading instance variables is a worthwhile method called Getter method (accessor)

In OC, the writing format of setter and getter method is stipulated.

The setter method is written in the following format:

-(void) Setage: (Nsinteger) age;

Note: The Set + instance variable name (capitalized), ignores underscores.

The Getter method is written in the following format:

-(Nsinteger) age;

Note: The return value type is always the same as the variable type, and the method is the same as the instance variable name, ignoring the underscore

Property

/*

Declaration of attributes: Use the @property Declaration property in the. h file.

Taking instance variable _name as an example

Declaration of a property

@property NSString *name;

Or

@property (NONATOMICC, retain) NSString *name;

The equivalent of two methods declared in @interface:

Setter method

-(void) SetName: (NSString *) name;

Getter method

-(NSString *) name;

Property implementation, using the @synthesize implementation property in the. m file

Take instance variable _name as an example:

Implementation of attributes

@synthesize name = _name;

Equivalent to the @implementation implementation of the

Setter method

-(void) SetName: (NSString *) name {

_name = name;

}

Getter method

-(NSString *) name {
return _name;

}

*/

In Xcode4.5 and later versions, @synthesize can be omitted, only the @property declaration attribute is allowed.

By default, the standard setter and Getter methods can be automatically generated by @synthesize when no setter and getter methods are implemented.

The function of a property is to generate the setter and the implementation of the Getter method, and if the instance variable of the method's internal operation is undefined, an instance variable of the property name is automatically generated, but the resulting instance variable visibility is private and the subclass is inaccessible.

. Once the setter, getter method is overridden, and no @synthesize is implemented, the @synthesize no longer generates the instance variable

Attributes of the property:

1. Literacy control (readonly. ReadWrite. setter=. getter=)

ReadOnly: The state of the system is to tell the compiler that the property only generates getter methods and does not generate getter methods.

ReadWrite: Read and write state, is to tell the compiler, the property both generate setter method and generate getter method, both the setup and accessor, the default read and write characteristics

Setter=: Specifies the name of the setter method generated by the property

Getter=: Specifies the name of the getter method generated by the property

2. Atomicity control (nonatomic. Atomic)

Atomic: Atomic characteristics, Setter, getter method in multi-threaded access under the absolute security, that is, setter, getter inside do multi-threaded access processing, the default atomic characteristics.

Nonatomic: Non-atomic characteristics, setter, getter method does not do multi-threaded access processing, just ordinary setter. Getter method.

(Nonatomic is typically used when declaring properties, and some properties are defined as atomic when thread safety is required)

3. Semantic features

Semantic settings (assign. Retain. copy)

If the attribute is a non-object type (such as int float, etc.) the semantic setting of the property is used Assgin

If the attribute is a semantic setting of an object type (such as nsstring, etc.), use retain.

If the property is an object type and wants a copy of the object, use copy.

OC-attributes, point syntax

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.