Objective-c----Setter and getter

Source: Internet
Author: User

Naming conventions:

For the naming of access methods, cocoa has its own conventions. When writing access methods for your own classes, you should follow these conventions so that you and others can read the code without being confused.
The setter method is named according to the name of the property it changes, plus the prefix set. Here are the names of several setter methods: Setengine:, SetStringValue:, SetFont:, Setfillcolor:, and Settextlineheight:.
The Getter method is named with the property name it returns. So the getter method corresponding to the setter method above should be engine, stringvalue, font, FillColor, and Textlineheight. Do not use get as a prefix for getter methods. For example, methods GetStringValue and GetFont violate the naming convention. Some languages, such as Java, have different naming conventions, and they use get to prefix the access method. But if you write a Cocoa program, please do not do so.

Description

The word get has a special meaning in Cocoa. If get appears in the method name of Cocoa, it means that the method will return the value as a pointer to the parameter you passed. For example, NSData (a class in cocoa, which can store a byte of any sequence) has a getBytes: method whose argument is the address of the memory buffer used to store bytes. The GetLineDash:count:phase in Nsbezierpath (for drawing Bezier curves): The method has three pointer parameters: a pointer to a floating-point array that stores a dashed-line style, and a pointer to the integer data that stores the number of elements in the dash style. And a pointer to the float data that points to the starting point of the dash.
If you use get in the name of the access method, the experienced Cocoa programmer will habitually pass the pointer as a parameter to this method, and when they find that this is just a simple access method, they will be confused. It's best not to let other programmers get confused by your code.

Example code:

. h file:

////Person.h//Oc_practice_03////Created by on 15/4/1.//Copyright (c) 2015. All rights reserved.//#import <Foundation/Foundation.h>  @interface  Person: nsobject {NSString*_name;NSString*_gender;int_age;}//setter Setup- (void) SetName: (NSString*) name;-(void) Setgender: (NSString*) gender;-(void) Setage: (int) Age;//getter Accessors- (NSString*) name;-(NSString*) gender;-(int) Age;//Initialize-(Instancetype) Initwithname: (NSString*) name Gender: (NSString*) Gender Age: (int) Age;-(void) eat;//Eat- (void) sleep;//Sleeping@end  // person

. m File:

////PERSON.M//Oc_practice_03////Created by on 15/4/1.//Copyright (c) 2015. All rights reserved.//#import "Person.h"  @implementation   person- (void) SetName: (NSString*) name {_name = name;}//SetName- (void) Setgender: (NSString*) Gender {_gender = gender;}//Setgender- (void) Setage: (intAge {_age = age;}//Setage- (NSString*) name {return_name;}//GetName- (NSString*) Gender {return_gender;}//Getgender- (int) Age {return_age;}//Getage-(Instancetype) Initwithname: (NSString*) name Gender: (NSString*) Gender Age: (int) Age {if( Self= [SuperInit]) {_name = name;        _gender = gender;    _age = age; }return  Self;}//Initwithname- (void) Eat {NSLog( @"%@ in the restaurant!" ", _name);}//Eat- (void) Sleep {NSLog( @"%@ is sleeping!" ", _name);}//Sleep@end  // person

Objective-c----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.