O-C Related -07-@property keywords introduction and use

Source: Internet
Author: User


basic concept: In O-c, after the class is created, you also need to add properties and methods to a class, and the previously mentioned set and get methods are cumbersome, so the @property compiler directive is introduced. @property is a compiler directive. the so-called compiler directive tells the compiler what to do with the content and what to do. @property tells the compiler to declare the reader (getter method and setter method)


1. in today's Xcode, @property can generate instance variables, declaration of methods, and implementation of methods at the same time.

2, rewrite Getter Method and Setter Method

Although @property is convenient, there will always be a day when the auto-generated method does not meet your needs. Therefore, you can override the Getter method or setter method, which satisfies the need for use.

Instead of overriding the parent-class method in the inheritance, the override here implements the setter method or getter method that you need, so that the system does not automatically generate it, and it does not write the method itself or is generated by the compiler. But you can't implement two methods at the same time.

As an example, you can use @property to replace the commented out part:

#import <Foundation/Foundation.h>


@interface Employee: NSObject

//(1) the name, age, and wage attributes in the class are used separately Char Array, int Variables and Double variable storage.

{

NSString * _NAME;

//

int _age;

//

Double _salary;

//


}

// Easy to use @property , replacing Set and the Get


@property nsstring * name;

@property int * age;

@property Double salary;

-(void) Mythod;




//// Design Show method.

-(void) show;

//// encapsulating three method sets

-(void) SetName: (NSString *) name;

-(void) Setage: (int) age;

-(void) Setsalary: (double) salary;

////getter Method

//

-(NSString *) name;

-(int) age;

-(double) salary;


@end


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

O-c related-[email protected] keywords Introduction and use

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.