• synthesize & property


    Using a declared property for the interface but implementing the property manually by writing the accessor implementations yourself is perfectly fine. It's important to the users of your class that your implementation match the semantics of the declared property, of course. If you manually implement the accessors, then you are responsible for implementing whatever details (such as an instance variable) that the accessor implemenations need.

    Alternatively, another way the compiler can help is to synthesize the implementation of the properties. There are two modes for property synthesis, automatic and explicit. If you neither manually implement the accessor methods nor "@synthesize" the property, then the compiler will automatically synthesize it. It will generate an instance variable for the property's storage, using the naming convention with the leading underscore, or use an instance variable of that name if you explicitly declared one. It will also generate the accessor methods with the proper semantics to match the @property declaration.

    If you explicitly synthesize a property with the @synthesize keyword, then the compiler will do something similar but slightly different. If your @synthesize statement doesn't explicitly associate an instance variable name with the property, then it will use a variable name without a leading underscore. So:

    @synthesize firstName;

    generates an instance variable named "firstName", not "_firstName", and uses that in the accessor implemenations it provides. To use the conventional name, you'd need to be explicit:

    @synthesize firstName=_firstName;

    The second difference is that explicit synthesis can be combine with manual implementation. The compiler will provide the pieces (instance variable, getter, setter) that you do not.

    https://developer.apple.com/forums/thread/69075

    @interface ViewController ()

    {

        NSString *string;

    }

    @property(nonatomic, strong) NSArray *array;

    @property(nonatomic, strong) NSDictionary *dic;

    @property(nonatomic, strong) NSString *string;

    /*

     //@synthesize string;

     Autosynthesized property 'string' will use synthesized instance variable '_string', not existing instance variable 'string'

     */

    @end

    @implementation ViewController

    @synthesize dic;

    //@synthesize string;

    - (void)viewDidLoad {

        [super viewDidLoad];

        // Do any additional setup after loading the view.

        _array = @[];

        dic = @{};

    }

  • 相关阅读:
    网页打开微信链接 无法返回
    wap尝试调取app(网易新闻为例)
    兼容性
    图片旋转效果
    a标签发送邮件
    windows 下的bash 环境安装npm
    css 设置滚动条的样式
    前端h5遇到的问题及解决办法
    safari input默认样式
    delphi 新版内存表 FDMemTable
  • 原文地址:https://www.cnblogs.com/feng9exe/p/13431926.html
Copyright © 2020-2023  润新知