self.object rather than calling the object directly within an object.self.object = foo; object = foo;
The difference between these two calls is that a call to
self.object will make use of the accessors generated by the @synthesize directive. A call to the object directly will bypass these accessor methods and directly modify the instance variable in question.It's important to realise that the dot syntax is a short hand for calling the accessor, so the two lines of code below are identical.
self.object = foo; [self setObject:foo]
Many developers would argue that you should never call the accessors from within the implementation of the object as this adds unnecessary overhead, and that if you override the default accessors to add addtional functionality you're obscuring the purpose of the code and that this is a bad thing.
Others argue that you should always use the accessors, and never access the instance variable directly. I generally stand with this camp, although I've been known to stray in the first camp from time to time.
Unless you understand what you're doing I'd recommed you access your instance variables, even inside the object that contains them, using the accessors by calling
self.object. There are a number of reasons why you might want to do this,- Extensibility - Using the accessor methods also give you the opportunity to override them if you need to in future and add additional functionality to your code. If you access the
objectdirectly you might face significant refactoring if you find yourself in the position where you need that capability. - Memory Management - Using the accessor methods ensures that the object retain count is set correctly.
- Key-Value Coding - If your application makes use of key-value coding it is essential that you use the generated accessor methods.
If you are using Core Data it is essential that you make use of the generated accessors, since many properties will not be loaded from the persistent store until they are needed. Accessing the instance variable directly will simply retun a
nil object.However you are generally safe to just use an object directly if you're only reading its value. If you're modifying the object, you should use the accessors in order to make sure that any other objects observing that property are properly notified. The exception to this is object initialisation and teardown, within which the accessors may not function correctly as the object itself may be in a paritally initialised state.
However if there are no objects observing the instance variable, there is no real problem in accessing the object directly. For instance key-value coding is not much used in iPhone development, being more commonly used when developing desktop applications, so this approach is more common amougst iPhone developers than those developers coming from an Objective-C background on the Mac.
In general my own coding style means that if I declare an object as a
@property I will use the synthesized accessor methods. However if I don't declare the instance variable as a property I won't, and will access the object directly. This does sometimes mean that if I later change my mind and declare an instance variable as a property the internal code in the object may access the variable directly instead of using the accessors. However unless I'm using key-value coding this does not necessarily mean that the code will result in an error, just that it should probably be refactored at a later date.In summary this is at least in part a matter of coding style, and like all such matters, there is not a cut-and-dried answer.
Learn more about this topic from Learning iPhone Programming.
Get the hands-on experience you need to program for the iPhone and iPod Touch. With this easy-to-follow guide, you'll build several sample applications by learning how to use Xcode tools, the Objective-C programming language, and the core frameworks. Before you know it, you'll not only have the skills to develop your own apps, you'll know how to sail through the process of submitting apps to the iTunes App Store.

Help




