Question

I've been sort of on the fence on this one for a while, but I'd like to know what people think about accessing instance variables directly from within an Objective-C class implementation?

Using accessors and mutators makes a lot of things easy, but for simple things, is it bad to access the instance variable directly? Is the best practice different for private instance variables?

Was it helpful?

Solution

If you always use your accessor methods, then you can be assured of memory management being handled properly, especially if those methods are synthesized. That being said, if we're talking about accessing ivars of primitive types, it doesn't hurt anything to use them directly within the implementation of your instance methods.

OTHER TIPS

I'm sorry, but I have to disagree to always using properties in a class to access ivars.

Usually if: 1. your property is not atomic, and 2. your property doesn't have complex logic (aka. you have only a synthesized property) - then it is unnecessary to use a property. Just use the ivar directly.

If your property is atomic, then obviously you MUST use both the getter and setter. However it is very handy to always use a setter ...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top