Best way of declaring private variables in cocoa

2019-01-24 19:48发布

问题:

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.

回答1:

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.