Reputation: 22939
When I declare the synthesis a property like this:
@syntesize myObject = _myObject;
I can access _myObject
directly in code, while avoiding the synthesized getters and setters.
However, if I synthesize the property like this
@syntesize myObject = myObject;
will I no longer be able to access the instance variable hidden behind = myObject
? Should I have a preference in using one or the other. Because I have often experienced "BAD_ACCESS
- problems" when using _myObject
instead of self.myObject
.
Upvotes: 0
Views: 227
Reputation: 21373
Using _myObject
in and of itself shouldn't cause memory management problems. The cause almost certainly lies somewhere else or is much more subtle than simply using an underscore prefixed ivar name. Anyway, if you name the ivar the same as the property, you can still access the instance variable by using just its name, as usual. It's only when you access it via the accessor methods (using e.g. either self.myObject
or [self myObject]
) that you're not directly accessing the ivar. You can also access it using self->myObject
, which is equivalent to just myObject
.
All that said, I consider it good practice to only access ivars directly inside the accessor method implementations themselves, along with init
(and dealloc
if not using ARC).
Upvotes: 1
Reputation: 124997
If you create a property, then you should use the property accessors and avoid using the instance variable directly. The only exceptions are in your -init
and -dealloc
methods, and in most cases it's fine to use the accessors there too. Don't avoid the accessors, use them.
Upvotes: 1
Reputation: 299275
In both cases you can access the underlying ivar. In both cases you should avoid doing so. Use accessors everywhere except init, dealloc and within accessors themselves. This will avoid many headaches, with or without ARC.
That said, if you're getting EXC_BAD_ACCESS
when accessing the ivar under ARC, you likely are doing one of the following:
__bridge
assign
or __unsafe_unretained
unsafelyUpvotes: 3