search

Home  >  Q&A  >  body text

objective-c - OC中间没有使用synthesize关键字,为什么还能访问呢?

#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关键字,怎么还是能调用呢?

天蓬老师天蓬老师2804 days ago441

reply all(5)I'll reply

  • 大家讲道理

    大家讲道理2017-04-24 09:16:05

    After using ARC, @synthesizeint _age; you can save everything and it will be added automatically. The code becomes elegant from now on!

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-24 09:16:05

    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.

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-24 09:16:05

    Because of @property, the getter setter method is automatically generated.

    reply
    0
  • 高洛峰

    高洛峰2017-04-24 09:16:05

    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.

    reply
    0
  • ringa_lee

    ringa_lee2017-04-24 09:16:05

    The compiler handles it automatically, no need to write sythesize

    reply
    0
  • Cancelreply