zoukankan      html  css  js  c++  java
  • 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 = @{};

    }

  • 相关阅读:
    阿里云oss怎么上传文件夹
    HTTP文件上传服务器-支持超大文件HTTP断点续传的实现办法
    让富文本编辑器支持复制doc中多张图片直接粘贴上传
    CSDN-markdown 文字样式设置(字体, 大小, 颜色, 高亮底色)
    网络编程基础知识点
    inet_addr 和inet_ntoa函数作用
    sockaddr和sockaddr_in的区别
    简析TCP的三次握手与四次分手
    TCP三次握手和四次挥手原理分析
    htons(), ntohl(), ntohs(),htons() 函数功能
  • 原文地址:https://www.cnblogs.com/feng9exe/p/13431926.html
Copyright © 2011-2022 走看看