OBJECTIVE-C Basic Notes (2) @property and @synthesize

Source: Internet
Author: User



First post the code in the previous article using @property and @synthesize, and then explain the usage and meaning of these two keyword, such as the following code:



Person.h file




#import <Foundation / Foundation.h>

@interface Person: NSObject {
     int _age; // can be accessed by subclasses
     // The system will help us generate a default int _no private variable (not accessible by subclasses)
}

@property int age;
@property int no;

// Write a constructor yourself
-(id) initWithAge: (int) age andNo: (int) no;

@end
Person.m File






#import "Person.h"

@implementation Person

// You don't need to write the following two sentences above Xcode 4.5 (can be omitted, and the default is _age and _no)
// @ synthesize age = _age; // declared as protected
// @ synthesize no = _no; // The default is private

-(id) initWithAge: (int) age andNo: (int) no {
     if (self = [super init]) {
         _age = age;
         _no = no;
     }
     return self;
}

-(NSString *) description {
     return [NSString stringWithFormat: @ "age is% i and no is% i", _age, _no];
}

@end
MAIN.M file






#import <Foundation / Foundation.h>
#import "Person.h"

int main (int argc, const char * argv []) {
     @autoreleasepool {
         Person * person = [[Person alloc] initWithAge: 15 andNo: 2];
        
         NSLog (@ "age is% i and no is% i", person.age, person.no);
        
         [person setNo: 3];
         NSLog (@ "no is% i", [person no]);
         //% @ represents printing an OC object
         NSLog (@ "% @", person);
     }
     return 0;
}
Output Result:







2014-11-12 21:53:15.406 firstocproj[826:47802] is 2



2014-11-12 21:53:15.407 firstocproj[826:47802] No is 3



2014-11-12 21:53:15.408 firstocproj[826:47802] is 3


You can see that the above code is a lot more concise, the function of @property is equivalent to the declaration of member variables, the role of @synthesize is equivalent to the standard implementation of the member variable setter and getter.








It is important to note that:



1, in Xcode4.5 above can not write @synthesize attribute (compiler Default).



2, such a writing can be used in the previous wording (old writing) cross-use.



3, assume that the setter or getter method has special treatment. can use the old notation (the compiler will not help us by default).



4. If you do not explicitly declare a variable, the default is to generate a private member variable (it cannot be used as a quilt, such as the _no above).






Here we will make the above code a little simpler:



Person.h




#import <Foundation/Foundation.h>

@interface Person : NSObject

@property int age;
@property int no;

@end
Person.m






#import "Person.h"

@implementation Person

@end
Main.m






#import <Foundation/Foundation.h>
#import "Person.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        Person *person = [Person new];
        [person setAge:20];
        [person setNo:3];
        NSLog(@"age is %i and no is %i", person.age, person.no);
    }
    return 0;
}
Output Result:







2014-11-12 22:14:44.002 firstocproj[849:52867] is 3


Note: My compiler Xcode is more than 4.5. So you can omit the @synthesize attribute in person.m.


Here has to be right OC and Xcode compiler exclamation. So convenient and easy to use tools I really was the first time to see, silently praise a bit.



OBJECTIVE-C Basic Notes (2) @property and @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.