2

I'm not familiar with the program language which has both property and instance variable.
So I don't know good ways to use property and instance variable.

Now I use only properties in my Objective-C code and I don't use any instance variables.

Do I need to use instance variable?
Or using only property is the best practice for Objective-C?

@interface ViewController : UIViewController
{
    // instance variable
    @public
    int a;
}

// property
@property(nonatomic, strong) NSString *b;

@end

7 Answers 7

8

The basic difference between variable and property is that, you can give attributes to property. You can not give any attributes to variable. So, if you wish to have any specific behavior like retaining the variable, accessing it atomically, have access out side the class, you should go for the properties.

If you simply want to access the variable with in the class and no special behavior is attached to that variable, no need to access it via property. You can directly use variable itself. It will improve the performance also.

Sign up to request clarification or add additional context in comments.

Comments

5

There are some advantages a @property has over an ivar:

  • Memory management : Behind the scenes it will create a setter which creates the variable with correct memory management. It will save you some headaches because you can easily see how the memory management is done (strong/weak and retain/copy/assign).

  • Accessibility from other classes: if you declare your @property in the .h and @synthesize it in the .m you ivar will be public readable and writeable. You can prevent this with a privat class extension. You even can declare a @property public readonly and declare them internally readwrite via a privat class extension. Eg: a private property

   // [In the implementation file]  
   @interface MyClass ()  
   @property (nonatomic, retain) NSMutableArray* someData; // private!!   
   @end  

   @implementation MyClass @synthesize someData   
   @end
  • Custom getter and setter: If you like you can still write custom getter and setters and you can even just write a getter or setter and let the other one automatically @synthesize. And you can write custom logic into such a getter and setter e.g. you can reload a tableview after a @property has changed.

  • Automatic Key-Value-Observing (KVO) compliant: If you use or planning to use KVO you get it basically for free by just declaring the property. Nothing else need to be done!

  • Dot notation: You can access getter and setter via dot notation if you have the @property.

   self.myIvar = (id) someThing; 
   [array addObject:self.myIvar];
  • If you need you iVar to be public it is simpler to write one @property than writing a getter and setter for a iVar

  • With a @property you do not need to declare in iVar (in iOS and 64bit Mac Os X applications). You can do it via the @synthesize:

    @synthesize myiVar = _myIvar;

Comments

4

Use properties everywhere. Don't even declare instance variables, but synthesize them like this: @synthesize myProperty = _myProperty in order to differentiate them from property names. Properties are good way to cope with memory management as well. The only place you must use the synthesized instance variable is in the dealloc method.
The advantages of the properties are a lot:
- The accessor methods define how will you get and set the value of your instance variable.
- You can customize the accessor methods (for example to lazy instantiate an ivar or do something when a setting a new value like setNeedsDisplay.
- You don't cope with memory management when setting a new value - the setter takes care for releasing/retaining (depending how have you declared the property - retain/copy/assign/strong.
- Some multithreading stuff with the atomic/nonatomic attributes
- You can take advantage of the KVO, when using properties
- And least, but not last - don't worry about performance issues if you have concernes that every time a getter or a setter is called...

4 Comments

I disagree here. Could you tell my why do you think it's not a good idea?
read answer below. and it is not cz some times you need to acces ivars too.
@graver thanks! I tried both using property everywhere and using property only when it is needed to be public. But I could write code more comfortably when i used property everywhere. Mixing ivar and property are very confusing for me.
Using properties in overridden setters might end up with infinite loop. Also using properties inside of inits is not recommended by the official docs. So those shouldn't be used everywhere. Take a look at : Access Instance Variables Directly from Initializer Methods
2

A @property is an instance variable that has had some semantic sugar applied to it, to help expose it to the outside world (usually), and to help avoid writing boilerplate code for getting and setting it.

Comments

0

though properties are made generally when you need to access some variable outside of the class, mean getter n setter, but in objective C, an additional need to make property is that the memory management goes on compiler ends, so if you are using some object, not primitive data types, then you should use property and synthesize it, and then release in dealloc if you are using manual reference counting. but again the main objective to make properties it to access some iVar outside the class like passing parameters from one class to other etc.

2 Comments

though properties are made generally when you need to access some variable outside of the class - this is the least reason they are made for. Properties are made to introduce rules on accessing and setting instance variables - something like a proxy to your ivar. Exposing your ivar outside your class happens thought methods (getter/setter later called) and it happens that property declarations actually create those methods for you - but their main intention and functionality is not to expose your ivar, but exactly to define how you get/set it.
iVar is always visible to own class, and the purpose of getter setter is to ensure the privacy of ivar that is data is private, and obviously we need to call getter or setter when we have not access to ivar and that is outside class not inside a class
0

If you @synthesize a @property, you will have access to a get and a set method, which are very convenient. You can also define how the setter will behave (retain, assign, copy) if it's nonatomic or atomic and if it's read only. If you don't specify anything (aka you don't synthesize the property) the ivar won't be visible outside of the class by default, but you can achieve this by using @public. Logically you can also define them as @private or @protected .

Normally I @synthesize a @property because I want to have access to the ivar from the outside of the class and also because I want a getter and setter methods.

Comments

0

The general opinion is that you should use properties whenever possible. If you're still in doubt, here is Apple's recommendation:

In general, you should use accessor methods or dot syntax for property access even if you’re accessing an object’s properties from within its own implementation, in which case you should use self.

...

The exception to this rule is when writing initialization, deallocation or custom accessor methods

...

You should always access the instance variables directly from within an initialization method because at the time a property is set, the rest of the object may not yet be completely initialized

Read the whole document here for a better understanding of the subject.

As for performance issues, for most apps the gain is insignificant. Read this for a very detailed description.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.