#import <Foundation/Foundation.h>
/**
默认的范围是protected
**/
@interface Animal :NSObject
{
int _age;
NSString * name;
}
@property int age;
@end
@implementation Animal{
}
@end
int main(int argc, const char * argv[]) {
Animal* animal=[Animal new];
animal.age=10;
[animal setAge:5];
NSLog(@"age is %d",animal.age);
return 0;
}
不是说synthesize关键字是在实现中自动生成set和get方法吗?但是我这里是没有使用
synthesize关键字,怎么还是能调用呢?
After using ARC,
@synthesize
和int _age;
you can save everything and it will be added automatically. The code becomes elegant from now on!This was not allowed in previous iOS versions. If I remember correctly, you can omit the synthesize keyword after iOS 7. As long as the property is set, the system will automatically generate the default get and set methods.
Because of @property, the getter setter method is automatically generated.
Contrary to @isteven's answer, the role of ARC is to provide automatic memory management, not to ignore @synthesize. The reason why @synthesize can be ignored is that every time a global variable is declared in the past, it needs to be resynchronized in it. Since this step has become a repetitive step, iOS development later omitted this step for the sake of humanization, and actually loaded this part automatically.
The compiler handles it automatically, no need to write sythesize