0

If an ivar is to be used globally within the class, but will never be accessed by other classes, are we still supposed to use properties?

Jano
  • 62,815
  • 21
  • 164
  • 192
TijuanaKez
  • 1,472
  • 3
  • 20
  • 28

2 Answers2

3

It is generally a good idea, as the generated accessors will take care of things like memory management and KVO for you. You can put the property in a class extension so other classes can't use it.

Chuck
  • 234,037
  • 30
  • 302
  • 389
  • So I guess it's not "necessary" but "good practice". I was using properties for everything but I noticed in apple example code some iVars are not declared as properties. I couldn't find any convention behind their choices. – TijuanaKez Mar 13 '12 at 05:18
  • Yes. The memory management benefits for objects in non-ARC environments is the big one, because it's just so easy to flub that when you don't concentrate that responsibility in a method. – Chuck Mar 13 '12 at 05:22
  • I would also mention that if your code contains `@private` directive for your iVars, you're probably doing something wrong. – Holly Oct 19 '13 at 19:00
1

For me, it depends on what the instance variable will be used for.

If it's an object representing some data, then I will always use a property.

If it's just a simple BOOL for some internal bookkeeping by a couple of methods in the class, then I won't create a property for it.

Abhi Beckert
  • 32,787
  • 12
  • 83
  • 110