1

There are some benefit of using variables? Or it is OK to use property instead of variable?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
zgjie
  • 2,099
  • 2
  • 21
  • 32
  • There are acute differences between a property and an ivar. It's a matter of encapsulation. – Jason Renaldo Aug 08 '13 at 15:44
  • There is an advantage to using properties (almost) exclusively, in that you have to spend less time thinking about how to manage them and how to access them. Folks often get their shorts in a knot, though, because it "breaks encapsulation", meaning that you (the sole programmer on your project) can access stuff you haven't authorized yourself to access. There's something to be said for encapsulation, but simplicity has a lot to recommend itself as well. – Hot Licks Aug 08 '13 at 15:58

2 Answers2

2

It's not only OK to use properties instead of instance variables in many situations, it's often the preferred choice.

Until ARC came along, one important aspect of properties (or accessors generally) was that they simplified memory management by reducing the number of places where an object needed to be retained or released. That's not so important now that we have ARC, but it gives some background as to why some people choose properties over ivars almost everywhere. Other reasons to choose properties include key value observing, consistent syntax for accessing object state, a concise way to describe the semantics of an attribute, and future-proofing through encapsulation.

There's an idea that accessor functions break encapsulation by exposing the inner working of an object, and I think that's often a reason that people choose to use ivars instead of properties. I disagree, though: you can always hide your properties in a class extension so that they're only visible to the class, and in any case accessors allow you to change the implementation while still providing the same interface. In any case, if you don't want users of your class to rely on a given attribute, you can either make it an ivar or you can make it a property that's declared in a class extension.

Another argument for ivars is speed -- using a variable directly avoids calls to accessor method. IMO, the other benefits of properties vastly outweigh any speed advantage in general. If there's a specific case where an accessor call is a real problem, there are usually better ways to solve the problem than avoiding properties.

Caleb
  • 124,013
  • 19
  • 183
  • 272
0

This question is kind of strange because, by default, all properties result in instance variables. So there can't be any benefit to using just ivars over properties.

If anything, properties have many benefits over ivars. Properties provide encapsulation, memory management (mostly relevant when using MRC), and KVO.

The only possible benefit of using a plain ivar over a property is slight efficiency. Directly accessing an ivar has less overhead than accessing a property (which is really a method call). But unless you have measured performance issues, the benefits of a property outweigh this.

The more important issue here is whether you made all of your properties public or not. Your .h file should only list public properties (those needed by other classes) and public method declarations. Everything else should be private in the .m file. Declare all of the private properties in a class extension in the .m file.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • Properties does have good encapsulation. When not changing the getter/setter of your not so private properties in the .m file to something special KVC can set/get them, even then you need to accessInstanceVariablesDirectly return NO, else KVC would set the ivar of the properties directly, even when the ivars are written explicitly and tagged as @private. – Binarian Aug 27 '13 at 11:20