Objective-c in the @property, @synthesize and other simple usage (eight)

Source: Internet
Author: User

Holydancer Original, if need to reprint, please indicate in prominent position:

transferred from Holydancer's csdn column, original address: http://blog.csdn.net/holydancer/article/details/7355833

In Objective-c, we can use new simple instead of alloc init, we present today is similar to the simple use of new OC features, with the @property, @synthesize to replace the Get,set method, it is very simple to use. Can save a lot of code, when the need to use the Set,get method of the place, we can use @property, @synthesize to a simple substitution, then the system will automatically give us the Set,get method of generating the variable, @property the declaration part of the corresponding method, @ Synthesize the implementation part of the corresponding method.

Human.h:

#import <Foundation/Foundation.h>

@interface human:nsobject
{
    int age;
    float height;
}
Here, the declaration of the Set,get method representing the age and height is represented by @property
@property int age;
@property float height;

@end

HUMAN.M:

#import "Human.h"

@implementation Human
//The implementation of Set,get method of age,height variable is represented here with @synthesize.
@synthesize age;
@synthesize height;

@end

MAIN.M:

#import <Foundation/Foundation.h>
#import "Human.h"
int main (int argc, const char * argv[])
{
    NSAutoreleasePool *pool=[[nsautoreleasepool Alloc]init];
    
    Human *human = [[Human alloc]init];
    “.” To the left of the equals sign, equivalent to the Set method.
    
    human.age=20;//equivalent to [human setage:20]
    human.height=60.50;
    
    “.” To the right of the equals sign, equivalent to the Get method.
    float tmp=human.height;//equivalent to float TMP = [human height]
    NSLog (@ "Age%d, height%f", human.age,tmp);
    
    [Human release];
    [Pool release];//is equivalent to performing one release for each object in the pool;
    

}

Output statement:

2012-03-15 10:11:34.052 string[325:403] Age , Height 60.500000

At this time the system will automatically generate Age,height Set,get method, can be used directly, there is a situation, if I only want to let age have get method, there is no set method to do, that is, in the initialization of a default value, and this value can only be called, cannot be modified, Is it possible to use @property this way? The answer is yes, because @property provides us with a similar parameter of an optional, the key word is readonly,readwrite, respectively, corresponding to only read can not modify and read and write can, generally we do not set ReadWrite, because the default is read and write state. Look at the code:

Human.h:

#import <Foundation/Foundation.h>

@interface human:nsobject
{
    int age;
    float height;
}
This is represented here in @property, which represents the declaration of the Set,get method of age and Height
@property (readonly) int age;
@property float height;

@end

HUMAN.M:

#import "Human.h"

@implementation Human
//Rewrite Init method to assign an initial value to age
-(ID) init
{
    if (self=[super Init])
    {
        age=20;
    }
    return self;
}
Here, @synthesize is used to represent the implementation of the Set,get method of the Age,height variable.
@synthesize age;
@synthesize height;

@end

MAIN.M:

#import <Foundation/Foundation.h>
#import "Human.h"
int main (int argc, const char * argv[])
{
    NSAutoreleasePool *pool=[[nsautoreleasepool Alloc]init];
    
    Human *human = [[Human alloc]init];
    human.height=60.5;
    human.age=30; If the line does not comment will be error, that is, age only get method, there is no set method
    NSLog (@ "age=%d", human.age);
    
   
    [Human release];
    [Pool release];//is equivalent to performing one release for each object in the pool;
    

}

Output:

2012-03-15 10:32:11.497 string[360:403] Age age=20


Keywords: objective-c, objective C, OC, @property, @synthesize

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.