Setter and getter of objective-C

Source: Internet
Author: User

1. General setter and getter statements

Setter and getter are the most basic things of a class. Any object-oriented language involves the concept of C ++ and Java. Because setter and getter are the most basic support for object-oriented language encapsulation.

In objective-C, the setter and getter are certainly not the same as general languages. However, some features are added.

For example, there is an instance variable: int age;

Declare the setter and getter in The. h file first.

-(void)setAge:(int)newAge;-(int)age;

And then implement it in the. M file

-(void)setAge:(int)newAge{    age=newAge;}-(int)age{    return age;}

It can be seen that in objective-C, the setter is no different, but the getter method name lacks get because get... in objective-C, the getter directly writes the variable name.

2. Call methods for getter and setter

The general call method is a traditional call method with brackets [], such

// For example, the above statement is a person class person * person = [[person alloc] init]; [person setage: 13]; int age = [person age];

Point call Method

// Call person. age = 13 ;//. the call appears on the left side of the = sign, which is equivalent to setterint age = person. age //. the call appears on the Right of =, which is equivalent to getternslog (@ "% I", person. age); // This is also Getter

3. Improved Writing of setter and getter

Every time you want to write getter and setter for an attribute, It is very troublesome, so there is a simpler way of writing,

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

@property int age;

Write it in the. M file to implement setteer and getter.

@synthesize age;

In this way, you can call getter and setter as before.

Iv. Improvement and optimization of setter and getter

As you can see, the method name of the getter is directly the variable name. The method name is the same as the variable name, which is easy to confuse. Therefore, this can be optimized.

In the. h file, the statement is still as follows:

@property int age;

In the. M file, write as follows,

@ Synthesize age = _ age; // Add a _ // so that we can use _ age and use age-(void) show {nslog (@ "% I", _ age );}

5. Attributes of @ Property

Attribute can be used to define @ property. The setting method is as follows:

@ Property (attribute1 [, attrubute2,...]).

For example:

@property (nonatomic,strong) Engine* engine;

If you set attributes in @ property, if you use @ synthesize, it will automatically help you implement these attributes. If you manually implement these attributes, then, you must write the implementation of these attributes by yourself.

(1) set the name of the Access Method

The default getter and setter names are associated with the variable names. They must be setvirablename and virablename. For example, the preceding variable Age, setter is setage, and getter is age.

You can set the setter and getter attributes in @ property to modify the method names of setter and getter.

Getter = gettername

Setter = settername

For example:

@ Property (getter = show1, setter = show2 :) int age; // now, the names of its getter and setter methods have changed

Note: If you set the readonly attribute, you should not set the setter attribute. Otherwise, a compiler warning will be given.

(2) set read-only or read/write

The following two attributes are well understood,

Readwrite: indicates both getter and setter.

Readonly: Only getter, no setter

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

(3) define the setter Semantics

The following attributes specify the setter semantics to set the accessors. They are mutually exclusive.

Strong: specifies that a strong (possess) relationship is related to the target object.

Weak: Specify whether weak (non-owning) is related 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. Replace it with the specified one ).

Copy: Call the copy () method of the original object to create a copy of the original object and assign it to a new reference. The original object is calling the release method. Of course, this attribute is only used to implement the object type of the nscopying protocol.

Assign: Specifies the setter that uses a simple value assignment. This property defaults. Use this attribute for scalar types (such as nsinteger and cgrect );

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

(4) thread security of access attributes

Nonatomic: indicates that thread security is not considered.

 

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.