在可可中声明私有变量的最佳方式 [英] Best way of declaring private variables in cocoa

查看:137
本文介绍了在可可中声明私有变量的最佳方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道在可可中声明私有实例变量的建议。这个问题是在iPhone上开发应用程序的上下文。



我知道至少有三种声明私有变量的方法:


  1. 使用修饰符@private在h文件的接口中声明它们:

      @interface MyClass:NSObject {
    @private
    NSObject * myPrivateVar;
    }


  2. 在m文件的实现部分声明: p>

      @implementation MyClass 
    NSObject * myPrivateVar;


  3. 在m文件的接口中声明一个属性(甚至不声明变量) :

      @interface MyClass()
    @property(nonatomic,retain)NSString * myPrivateVar;
    @end
    @implementation
    @synthesize myPrivateVar;


到目前为止,最近才意识到这可能是危险的,由于缺乏垃圾收集。是否有使用该方法完全可以接受的情况?



3更合适吗?是否答案取决于对象类型(例如mutable / immutable)?



指向参考资料讨论使用/不使用属性的权衡一般也赞赏。 / p>

解决方案

您的三个选项有不同的语义:


  1. 这会创建一个实例变量。没有垃圾回收你需要保留 / 发布对象你存储到 myPrivateVar

  2. 不会定义所有的实例变量。在 @interface 之外定义的变量以及许多方法(或函数)定义的范围是全局 - 有效的类变量(Objective-C没有特殊语法) 。这样的变量由 MyClass 的所有实例共享。

  3. 使用属性(有或没有显式声明)归结为内存管理。定义为 retain 表示不需要 retain / release

因此不要使用2!选项3显然有好处,如果你没有垃圾收集,它提供了一些抽象的选项1,并且是更昂贵的 - 虽然你可能不会注意到计算密集型代码之外的访问变量的重大差异。 p>

更新2015



上面使用了垃圾回收 (自动引用计数)现在更适用(垃圾回收现已废弃)。现在还有第四个选项:


  1. 在m文件的实现部分声明它们: / p>

      @implementation MyClass 
    {
    NSObject * myPrivateVar;
    }

    与选项(2)不同,它声明了一个实例变量。该变量对实现是私有的,并且与ARC内存管理是自动的。这个和(3)[偶然也不再需要 @synthesize ]之间的选择取决于选择和需要;属性给你点语法,自定义setter和/或getter的能力和 copy 属性,但如果你不需要这些,你可以简单地请使用实例变量。



I would like to know what the recommendations are for declaring private instance variables in cocoa. This question is in the context of developing apps on the iPhone.

I am aware of at least three ways of declaring private variables:

  1. Declare them in the interface of the h file with the modifier @private:

    @interface MyClass : NSObject {  
      @private  
      NSObject * myPrivateVar;   
    }
    

  2. Declare them in the implementation section of the m file:

    @implementation MyClass  
    NSObject * myPrivateVar;
    

  3. Declare a property in the interface of the m file (not even declaring the variable itself):

    @interface MyClass ()  
    @property (nonatomic, retain) NSString* myPrivateVar;  
    @end  
    @implementation  
    @synthesize myPrivateVar;
    

So far, I have used extensively 2 but recently came to realize this might be dangerous due to the lack of garbage collection. Are there cases where it remains perfectly acceptable to use that method?

Is 3 more appropriate? Does the answer depend on the object type (e.g. mutable/immutable)?

Pointers to reference material discussing the trade offs for using/not using properties in general also appreciated.

解决方案

Your three options have different semantics:

  1. This creates an instance variable. Without garbage collection you need to retain/release objects you store into myPrivateVar.
  2. This does not define an instance variable at all. Variables defined outside of the @interface and the scope of many method (or function) definitions are "global" - effectively class variables (which Objective-C has no special syntax for). Such a variable is shared by all instances of MyClass.
  3. The difference between using a property (with or without the variable being explicitly declared) comes down to memory management. Defined as you have with retain means that there is no need for retain/release when you do not have garbage collection.

So don't use 2! Option 3 clearly has benefits if you don't have garbage collection, it provides some measure of abstraction over option 1, and is more costly - though you will probably not notice the difference outside of computationally intensive code which access the variable heavily.

Update 2015

Where garbage collection is used above ARC (automatic reference counting) is now more applicable (garbage collection is now deprecated). Also there is now a fourth option:

  1. Declare them in the implementation section of the m file:

    @implementation MyClass  
    {
       NSObject * myPrivateVar;
    }
    

    Unlike option (2) this does declare an instance variable. The variable is private to the implementation, and with ARC memory management is automatic. The choice between this and (3) [which incidentally also no longer requires the @synthesize] comes down to choice and need; properties give you dot syntax, the ability to customise the setter and/or getter, and the copy attribute for automatic copy on assignment, but if you need none of these you can simply use the an instance variable.

这篇关于在可可中声明私有变量的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆