Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
446 views
in Technique[技术] by (71.8m points)

properties - Best way of declaring private variables in cocoa

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...