Objective-c's setter and getter

Source: Internet
Author: User

I. General wording of setter and getter

Setter and getter can be said to be a class of the most basic things, any object-oriented language, all this concept, C + +, Java and so on. Because setter and getter are the most basic support for object-oriented language encapsulation.

In the Objective-c setter and getter, of course, and the general language is no different. Only, add some of your own features.

For example, there is an instance variable:

The setter and getter are declared in the. h file First

@property (nonatomic,retain) int age;

-(void) Setage: (int) newage;

-(int) age;

It is then implemented in the. m file specifically

-(void) Setage: (int) newage

{

if (_age! = newage)
{
[NewAge retain];
[_age release];
_age = NewAge;
}

}

-(int) age

{

return _age;

}

-(void) dealloc
{
[_age release];
[Super Dealloc];
}

As you can see, the setter in the objective-c is no different, but the getter's method name is missing get, because get ... The objective-c is different from other uses, so the getter directly writes the variable name.


Second, getter and setter of the call method

The general method of invocation is the traditional calling method with brackets [], such as

For example, the above statement is a person class

person* Person=[[person Alloc]init];

[Person setage:13];

int Age=[person Age];

How the point is called

Point callperson.age=13; The. Call appears to the left of the = number, equivalent to setter

int age=person.age//. The call appears to the right of the = number, which is equivalent to getter

NSLog (@ "%i", person.age);//This is also getter


Iii. improved wording of setter and getter

Every time you write a getter and setter for a property, you have to have a lot of trouble with your hands, so there's a simpler notation,

In the. h file, write this directly, indicating that an instance property and its getter and setter are declared

@property int age;


This is then written in the. m file, indicating implementation of Setteer and getter

@synthesize age;

This allows the getter and setter to be called as before.


Iv. improved optimization of setter and getter

It can be seen that the method name of the getter is directly the variable name, the method name and variable name, easy to confuse people, so, can be optimized.

This is still stated in the. h file

@property int age;

In the. m file, so to write,

@synthesize age=_age;//Add a _

So, we can go to use _age and use age as

-(void) show

{

NSLog (@ "%i", _age);

}


V. Properties of the @property

You can use attributes to specify @property in the following ways:

@property (Attribute1[,attrubute2,...] )。

As an example:

@property (Nonatomic,retain) engine* Engine;

If you set a property in @property, if you use @synthesize, then it will automatically help you with the implementation of these properties, and if you do it yourself manually, then you must write the implementation of these attributes yourself.


(1), set the name of the access method

The name of the default getter and setter is associated with the variable name, which must be setvirablename and virablename, such as the variable above Age,setter setage,getter is age.

You can modify the setter and getter method names by setting the setter and Getter properties in @property.

Getter=gettername

Setter=settername

As an example:

@property (getter=show1,setter=show2:) int age;//Now, its getter and setter method names change.

Note: If you set the ReadOnly property, you should not set the Setter property, or you will be given a compiler warning.


(2), set read-only or read-write

The following two properties are well understood,

ReadWrite: Indicates both getter and setter

ReadOnly: Indicates only getter, no setter

These two properties are mutually exclusive and can only exist one.


(3), define the semantics of setter

The following property specifies the setter semantics settings accessor. They are mutually exclusive.

Strong: Specifies that there is a strong (owning) relationship to the target object.

Weak: Specifies that there is a weak (non-owning) relationship to the target object. If the destination object is destroyed, the property value is automatically set to nil. (The weak attribute does not support v10.6 and iOS 4 on OS X and is replaced with the designation).

Copy: Invokes the copy () method of the original object, creating a copy of the original object to assign to the new reference. The original object is called in the release method. Of course this attribute is only used to implement the object type of the Nscopying protocol.

Assign: Specifies a setter that uses a simple assignment.  This attribute is a default. Use this property for scalar types (such as Nsinteger and cgrect, etc.);

Retain: Specifies that the retain should be called on the object. The original object is called release. In OS X v10.6 and after, you can use this keyword for memory management.


(4), thread safety for accessing properties

Nonatomic: Indicates that thread safety is not considered

Objective-c's setter and getter

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.