Objective-C setter, the relationship between getter and instance variables, attributes, and point syntax

Source: Internet
Author: User

When setter, Getter,

  • In oC, the method for assigning Zhi to the instance variable is called setter)

  • The method used to read the instance variable value is called getter)

  • The value assignment methods we wrote earlier can be called setter and getter.

Setter and getter writing formats

  • The writing format of setter and getter in oC

  • If an instance variable is int age; or int_age;

  • Setter's writing format is as follows-(void) setage :( INT) age; that is, the instance variable name with the upper Letter of the set + initial letter (ignore the underline)

  • The getter writing format is as follows:-(INT) age; that is, the return value type is the same as the variable type, and the method name is the same as the instance variable name (ignore the underline)

Relationship between setter and getter and instance variables

  • Whether setter or getter is internally operating on instance variables

  • Each instance variable requires a pair of setter and getter methods.

Attribute Definition

  • Attribute Declaration: Use @ property to declare attributes (for example, @ property nsstring * Name ;)

  • Equivalent to two methods in @ Interface

  • -(Void) setname :( nsstring *) Name;

  • -(Nsstring *) Name;

Attribute implementation

  • Attribute implementation: Use @ synthesize to implement the attribute (for example, @ synthesize name = _ name)

  • Equivalent to the implementation of @ implementtation

  • -(Void) setname :( nsstring *) Name;

  • -(Nsstring *) Name;

Attribute attributes

  • Objective-C provides attributes to simplify programmer coding.

  • Some keywords are provided for attributes to control the implementation details of setter and getter.

  • These keywords are called attributes)

  • There are three categories of attributes.

There are three types of attributes.

Category 1

  • 1. Read/write control (readonly, readwrite, setter, Getter)

  • If the keyword of the read-write control is readonly, it tells the compiler that only the getter method is declared (no setter method)

For example, @ property (readonly) nsstring * Name; equivalent to-(nsstring *) Name;

  • If it is readwrite, tell the compiler that the setter Declaration has the getter declaration.

Example: @ property (readwrite) nsstring * Name;

It is equivalent to-(nsstring *) Name;

-(Void) setname :( nsstring *) Name;

  • Readwrite is the default setting for read/write control.

Category 2

  • 2. Atomic control (nonatomic, atomic)

  • If the keyword of atomic control is atomic. The setter and getter methods are absolutely secure under multi-thread access, that is, the setter and getter methods implement multi-thread access processing internally, and the default setting of atomic control is atomic;

  • If the atomic control keyword is nonatomic, the setter and getter methods do not implement multi-threaded access processing internally. They are just common multi-setter and getter methods.

..

  • In the process of program development, setter and getter are used everywhere. If atomic is used, it is necessary to constantly lock and unlock setter and getter to ensure thread access security and occupy system resources, reduces system performance.

  • It is set to nonatomic in the same city. Some attributes are defined as atomic only when thread security is required.

Example: @ property (readwrite, nonatomic) nsstring * Name;

Equivalent to;-(nsstring *) Name;

-(Void) setname :( nsstring *) Name;

Category 3

  • 3. semantic settings (assign, retain, copy)

  • If the semantics is set to assign. The internal implementation of setter and getter is direct assignment,

Example: @ property (nonatomic, assign) nsstring * Name;

-(Void) setname :( nsstring *) Name

{

_ Name = Name;

}

-(Nsstring *) Name

{

Return _ name;

}

  • If the keyword set in the semantics is retain. The internal implementation of setter and getter will optimize the memory.

Example: @ property (nonatomic, retain) nsstring * Name;

-(Void) setname :( nsstring *) Name

{If (_ name! = Name ){

[_ Name release];

_ Name = [name retain];

}

}


-(Nsstring *) name {

Return [[_ name retain] autorelease];


}

  • If the keyword set in the semantics is copy, the internal implementation of setter and getter will also optimize the memory.

For example, @ porperty (nonatomic, copy) nsstring * Name;

-(Void) setname :( nsstring *) Name

{

If (_ name! = Name ){

[_ Name release];

_ Name = [name retain];

}

}

-(Nsstring *) Name

{

Return [[_ name retain] autorelease];

}

Note that

  • If attributes are non-object type (such as int and float) attributes and Other semantic settings, only use assign.

  • If the attribute is of the object type (such as nsstring and nsarray ),

  • If the property is of the object type and you want to get the copy parameter, use the copy keyword.

-Usage of point syntax

  • Point syntax can be used for all methods that conform to the system's default setter and getter writing formats.

For example: [person setname: @ "zhangsan"];

It can be equivalent to "person. Name = @" zhangsan ";


Nsstring * name = [person name];

It can be equivalent to nsstring * name = person. Name;

  • The property is a pair of getter and setterfangfa, And the dot syntax is another calling format of the property.


This article from the "Liu _ blog" blog, please be sure to keep this source http://liuyafang.blog.51cto.com/8837978/1543715

Objective-C setter, the relationship between getter and instance variables, attributes, and point syntax

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.