zoukankan      html  css  js  c++  java
  • difference between instance variable and property

    @interface MyClass : NSObject {
        NSString *name;
        NSArray *items;
    
        Something *something;
    
        IBOutlet NSTextField *myTextField;
    }
    
    @property (nonatomic, retain) NSString *name;
    @property (nonatomic, retain) NSArray *items;
    

      

    in the iPhone world, there's no garbage collector available. You'll have to carefully manage memory with reference counting. With that in mind, consider the difference between:

    name =@"Test";

    and

    self.name =@"Test";// which is equivalent to:[self setName:@"Test"];

    If you directly set the instance variable, without prior consideration, you'll lose the reference to the previous value and you can't adjust its retain count (you should have released it manually). If you access it through a property, it'll be handled automatically for you, along with incrementing the retain count of the newly assigned object.

    The fundamental concept is not iPhone specific but it becomes crucial in an environment without the garbage collector.

    Declaring ivar via just declaring a property for it is a new language feature available starting objc 2.0

    In "Run-time differences" section of "Objective-c programming language" reference stated:

    For @synthesize to work in the legacy runtime, you must either provide an instance variable with the same name and compatible type of the property or specify another existing instance variable in the @synthesize statement. With the modern runtime, if you do not provide an instance variable, the compiler adds one (_propertyName) for you.

  • 相关阅读:
    alipay h5支付接口总结
    ztree js 和父子节点递归 使用注意
    .net framework to mono 绿色运行摘记
    curl 命令行用法摘记
    C# Camera2 实现扫描识别二维码及Texture预览
    .net framework 3.5 安装命令
    android 调用相机
    Xamarin Android 定时刷新UI
    Xamarin 动态申请 android 权限
    C# 生成和识别二维码
  • 原文地址:https://www.cnblogs.com/fengjian/p/3309406.html
Copyright © 2011-2022 走看看