objective-c - OC中间没有使用synthesize关键字,为什么还能访问呢?
天蓬老师
天蓬老师 2017-04-24 09:14:05
0
5
436
#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关键字,怎么还是能调用呢?

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

reply all(5)
大家讲道理

After using ARC, @synthesizeint _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.

Peter_Zhu

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template