Setter Getter and properties

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, there is 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: int age; The setter and getter naming rules are declared in the. h file: setter-(void) Set variable name (first capitalization): Parameter name (member variable type), getter-(member variable type) member variable name; [Plain]View Plaincopy
    1. -(void) Setage: (int) newage; Named according to the variable name you changed (for assignment)
    2. -(int) age; Getter is named (for value) based on the variable name obtained

It is then implemented in the. m file specifically

[Plain]View Plaincopy
    1. -(void) Setage: (int) newage
    2. {if (newage>100| | NEWAGE<0)
    3. {
    4. NSLog (@ "Input age is unreasonable, please re-assign value!) ”);
    5. }else
    6. Age=newage;
    7. }
    8. -(int) age
    9. {
    10. return age; has a return value;
    11. }
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

[HTML]View Plaincopy
    1. For example, the above statement is a person class
    2. person* Person=[[person Alloc]init];
    3. [Person setage:13];
    4. int Age=[person Age];
The point invocation can only be used if the setter and getter methods are defined in the standard format. Expression (Strict standard: A member variable requires its own unique set and get method, a set can only assign a value to a member variable!) ) [HTML]View Plaincopy
    1. Point call
    2. person.age=13; The. Call appears to the left of the = number, equivalent to setter
    3. int Age=person.age//. The call appears to the right of the = number, which is equivalent to getter
    4. 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

[Plain]View Plaincopy
    1. @property int age;
This is then written in the. m file, indicating implementation of Setteer and getter [Plain]View Plaincopy
    1. @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

[HTML]View Plaincopy
    1. @property int age;
In the. m file, so to write, [HTML]View Plaincopy
    1. @synthesize Age=_age; Add a _
    2. So, we can go to use _age and use age as
    3. -(void) show
    4. {
    5. NSLog (@ "%i", _age);
    6. }

V. Properties of the @property

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

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

As an example:

[Plain]View Plaincopy
    1. @property (Nonatomic,strong) 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:

[HTML]View Plaincopy
    1. @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: class Interface: #import <Foundation/Foundation.h>
@interface Audicar:nsobject
{NSString *color;
float price;
int seat;
}
-(void) SetColor: (NSString *) Newcolor;
-(void) Setprice: (float) newprice;
-(void) Setseat: (int) newseat;
-(nsstring*) color;
-(float) Price;
-(int) seat; @end class implementation: #import "AudiCar.h"
@implementation Audicar
-(void) SetColor: (NSString *) Newcolor
{
color = Newcolor;
}
-(void) Setprice: (float) newprice
{
Price = Newprice;
}
-(void) Setseat: (int) newseat
{
Seat = Newseat;
}
-(nsstring*) color
{
return color;
}
-(float) Price
{
return price;
}
-(int) seat
{
return seat;
} @end Main program: #import <Foundation/Foundation.h>
#import "AudiCar.h"
int main (int argc, const char * argv[])
{@autoreleasepool
{
Audicar *car = [[Audicar alloc]init];
[Car setcolor:@ "yellow"];
[Car setprice:45000.350000];
[Car setseat:7];
NSLog (@ "COLOR:%@", [car color]);
NSLog (@ "Price:%.2f", [car prices]);
NSLog (@ "seat:%d", [car seat]);
}
return 0;} Operation Result: 2015-10-21 10:57:29.098 10.21 Morning class Exercises [996:43363] the colors are: Yellow
2015-10-21 10:57:29.099 10.21
Morning class Exercises [996:43363] prices are: 45000.35 2015-10-21 10:57:29.100 10.21 Morning class exercise [996:43363] seat: 7

Setter Getter and properties

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.