@synthesize & @property Xcode 4.4之前...

来源:互联网 发布:php 比特比交易平台 编辑:程序博客网 时间:2024/05/20 17:23

main.m

#import "Person.h"#import <Foundation/Foundation.h>int main(int argc, const char * argv[]) {@autoreleasepool {Person * p = [[Person alloc] init];        p.age = 18;        NSLog(@"%d",p.age);        [p test];}return 0;}// Autosynthesized property 'name' will use synthesized instance variable '_name', not existing instance variable 'name'// Autosynthesized property 'age' will use synthesized instance variable '_age', not existing instance variable 'age'


Person.h

#import <Foundation/Foundation.h>@interface Person : NSObject{    @public    NSString * _name;    int _age;    int _number;}@property int age;-(void)test;@end


Person.m

#import "Person.h"@implementation Person//@synthesize age = _number;@synthesize age=_age; // 如果不写=_age ,系统自动生成age变量,_age就赋值不了了./** 错误写法*///-(void)setAge:(int)age//{//    _number = age;//}//-(int)age//{//    return _number;//}/** 正确写法 *///-(void)setAge:(int)age//{//    self->age = age;//}//-(int)age//{//    return self->age;//}- (void)test {NSLog(@"%d", age); // 18NSLog(@"%d", _age); // 0}@end


0 0
原创粉丝点击