Third day of iOS (@property and @synthesize usage)

Source: Internet
Author: User

First, @property and @synthesize Basic specification usage

[Email protected]

When the compiler encounters @property, it is automatically expanded into getter and setter declarations

#import<Foundation/Foundation.h>@interfaceStudent:nsobject {int_age; int_no; float_height;}//when the compiler encounters @property, it is automatically expanded into getter and setter declarations@propertyintAge ;//-(void) Setage: (int) newage;//-(int) age;@propertyintNo;//-(void) Setno: (int) newno;//-(int) No;@propertyfloatheight;//-(void) SetHeight: (float) newheight;//-(float) height;- (void) test;@end

[Email protected]

@synthesize will automatically generate getter and setter implementations

#import "Student.h"@implementationStudent//@synthesize Age, Height, no;//@synthesize will automatically generate getter and setter implementations//@synthesize default to access variables with the same name as age//if a variable with the same name is not found, a private variable with the same name is generated automatically//@synthesize age;//age = _age represents getter and setter will go to access _age This member variable@synthesizeAge =_age;//-(void) Setage: (int) NewAge {//_age = newage;//}////-(int) Age {//return _age;//}@synthesizeHeight =_height;//-(void) SetHeight: (float) newheight {//_height = newheight;//}////-(float) Height {//return _height;//}@synthesizeNo =_no;//-(void) Setno: (int) Newno {//_no = Newno;//}////-(int) No {//return _no;//}- (void) Test {_age=Ten; _height=10.0f; _no=Ten; }@end

Second, @property and @synthesize advanced usage

Person.h

#import <Foundation/Foundation.h>@interface  person:nsobject {    int _id;     float  intfloat  weight; @end

Person.m

#import "Person.h"@implementation Person@synthesizeID =_id;@synthesizeWeight =_weight;//-(void) Setweight: (float) Weight {//_weight = weight *;//}//-(float) Weight {//return _weight *;//}@end

Third, @property and @synthesize Ultimate usage

Teacher.h

#import <Foundation/Foundation.h>@interfaceint age ; @end

Teacher.m

#import " Teacher.h " @implementation Teacher // in the xcode4.5 environment, @synthesize can be omitted and the _age member variable is accessed by default // if the _age member variable is not found, a private member variable called _age is automatically generated -(void) test {        ten;} @end

Third day of iOS (@property and @synthesize usage)

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.