IOS development-OC language (6): syntax and @ property,-oc @ property

Source: Internet
Author: User

IOS development-OC language (6): syntax and @ property,-oc @ property

Point syntax and @ property

 

Knowledge Point

 

1. setter/getter Function

2. Point syntax

3. @ property syntax and attributes

 

 

==========================================================

1. setter and getter Functions

1. Functions of setter and getter

Setter method: Modify the object field/instance variable

Getter method: Read the object field/instance variable

 

Setter method: it can contain multiple parameters and assign values to multiple variables at the same time.

 

Getter method: only one variable value can be returned without parameters.

 

 

 

2. How to name setter and getter?

Setter Method Name:

Xxx: indicates the name of the member variable.

 

// With a parameter

-(Void) setXxx: (parameter type) parameter 1;

// With multiple parameters

-(Void) setXxx :( parameter type) variable name xxx :( parameter type) variable name ......;

 

 

Getter Method Name:

-(Return Value Type) xxx;

 

 

========================================================

Ii. Point syntax to simplify Program Design

Hides memory management details

Hiding multithreading, synchronization, and locking

 

 

2. Functions of point syntax

The property can use the dot syntax when the object method name is not applicable.

You can call foo. value to access [foo value ].

Although foo. value looks like directly accessing the value variable, the attribute is always called.

Methods, and these methods can access the object data.

 

 

3. Point syntax call

// You can use the point syntax to assign values to the set Method of a parameter.

[Xiaoxin setName: @ ""];

// This Is Not A member variable, but the set method.

Xiaoxin. name = @ "xiaoxin ";

// You can call the get method using the. syntax.

NSString * ret = [xiaoxin name];

// Xiaoxin. name indicates the set Method on the left of the equal sign. The value of this expression is the get method.

Ret = xiaoxin. name;

[Note] the dot syntax only accesses the setter getter method, rather than using member variables.

 

4. Relationship between point syntax and setter/getter Functions

 

 

The dot syntax is essentially the same as the [] syntax. It also sends set and get messages, but it is written differently.

Dog. name = @ "xiaobai ";

Expand to [dog setName: @ "xiaobai"];

 

 

NSString * nameString = dog. name;

Expanded to NSString * nameString = [dog name];

========================================================

3. Attribute (@ property) and synthesis (@ synthesize)

 

1. When there are many member variables in a class, Will setter and getter appear very bloated? @ property and @ synthesize can make the code concise.

 

2. @ property usage

1) Let the compiler automatically declare setter and getter

3. @ property usage

1) @ property member variable type variable name;

 

 

4. @ synthesize usage

1) Let the compiler automatically implement the setter and getter Functions

 

 

5. @ synthesize can be omitted (Xcode4.6 or later)

// Declare an attribute, which is equivalent to declaring the following two methods

@ Property NSString * name;

//-(Void) setName :( NSString *) name;

//-(NSString *) name;

 

@ Synthesize name = _ name;

// Before Xcode4.6, you must implement the following two methods:

// Starting from Xcode4.6, @ synthesize does not need to be written. The following two methods are automatically implemented and the variables of name_xxx are automatically associated.

// If no corresponding variable exists in the class, the corresponding variable is automatically generated.

 

==============================================

@ Property

 

1. atomic operation

Atomic (default)/nonatomic

 

 

 

2. assign directly assigns a modifier (default), which is generally used for basic types.

// Default (default) Modifier

@ Property (assign) NSString * name;

// Generate both the set and get Methods

 

Directly assign values. The expanded set method is as follows:

-(Void) setName :( NSString *) name

{

_ Name = name;

}

 

3. read/write operations

Readonly/readwrite (default)

// Read-only Modifier

// Generate only getter without setter

@ Property (readonly) NSUInteger age;

 

/Default Modifier

// Generate both the set and get Methods

@ Property (readwrite) NSString * name;

 

 

 

4. Alias the set and get methods.

// Getter Modifier

// Alias the get Method

@ Property (getter = myWeight) NSUInteger weight;

 

 

// Setter Modifier

// Alias the set Method

@ Property (setter = setLife :) BOOL alive;

 

// You can add multiple modifiers to an attribute. Multiple modifiers are separated by commas.

@ Property (setter = setUsername:, getter = getUsername) NSString * name;

 

 

 

5. Multiple Attribute modifiers separated by commas

@ Property (nonatomic, readonly, getter = birth)

NSString * birthday;

 

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.