iOS Development-oc language (vi) DOT syntax and @property

Source: Internet
Author: User
Tags modifiers

Dot Syntax and @property

Knowledge points

1.setter/getter function

2. Dot syntax

[Email protected] syntax and properties

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

First, setter and getter functions

The role of 1.setter and getter functions

Setter method: Modify the object's field/instance variable

Getter Method: Read the field/instance variable of an object

Setter method: Can have multiple parameters, you can assign values to multiple variables at the same time

Getter method: The value of only one variable can be returned without arguments.

How 2.setter and Getter are named

Name of setter method:

XXX: Represents the name of the member variable

With one parameter

-(void) Setxxx: (parameter type) parameter 1;

With multiple parameters

-(void) Setxxx: (parameter type) variable name XXX: (parameter type) variable name ...;

Name of getter Method:

-(return value type) xxx;

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

Second, point Grammar in order to simplify the programming

Memory management details are hidden

Multi-threaded, synchronous, lock-out hidden

2. The role of Dot grammar

property can use point syntax when the object method name is not applicable

You can call Foo.value to access without calling [Foo value]. Notice the difference with the structure

Although Foo.value looks like a direct access to the value variable, the property always calls the

Methods, and these methods can access the object's data.

3. Invocation of Point syntax

A set method of a parameter can be assigned using DOT syntax

[Xiaoxin setname:@ "small New"];

This is not using member variables, it is using the Set method

Xiaoxin.name = @ "small new";

You can call the Get method through the. syntax

NSString * ret = [xiaoxin name];

Xiaoxin.name on the left side of the equals sign is the set method, which uses the value of the expression, which is the Get method

ret = Xiaoxin.name;

The "note" point syntax simply accesses the setter getter method, not using a member variable.

4. The relationship between point syntax and Setter/getter function

"Point syntax and [] notation" are essentially the same as sending set and get messages, but with different wording.

Dog.name = @ "Xiaobai";

Expand into: [Dog SetName: @ "Xiaobai"];

NSString *namestring = dog.name;

Expanded into: nsstring *namestring = [dog name];

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

Iii. attributes (@property) and composition (@synthesize)

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

[Email protected] useful

1) Let the compiler automatically declare the setter and Getter

[Email protected] Use

1) @property member variable type variable name;

[Email protected] useful

1) Let the compiler automatically implement setter and getter functions

[email protected] can be omitted (Xcode4.6 can be omitted later)

Declaring a property is equivalent to declaring the following two methods

@property NSString * NAME;

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

-(NSString *) name;

@synthesize name = _name;

Xcode4.6 before, it needs to be implemented as follows two methods

Xcode4.6, no need to write @synthesize, automatic implementation of the following two methods, will automatically close the joint bit _xxx variables

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

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

@property Properties

1. Atomic operation

Atomic (default)/nonatomic

2. Assign direct assignment modifier (default), generally used in basic type.

Default (default) modifier

@property (assign) NSString * name;

Generate a set, get method at the same time

Direct assignment, expanded set method:

-(void) SetName: (NSString *) name

{

_name = name;

}

3. Read and write operations

Readonly/readwrite (default)

Read-only modifier

Generate getter only, no setter

@property (readonly) Nsuinteger age;

/default modifier

Generate a set, get method at the same time

@property (ReadWrite) NSString *name;

4. Alias the set method and the Get method

Getter modifier

To alias a Get method

@property (getter = myweight) Nsuinteger weight;

Setter modifier

To alias a set method

@property (setter = Setlife:) BOOL alive;

A property can add multiple modifiers, separated by commas between multiple modifiers.

@property (setter = Setusername:, getter = getusername) NSString *name;

5. Multiple attribute modifiers that need to be separated by commas

@property (nonatomic, readonly,getter = birth)

NSString * Birthday;

iOS Development-oc language (vi) DOT syntax and @property

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.