Question

Ok, I am confused! I used to use -> whenever I accessed my instance objects, but now I see that after I set them in my application:didFinishLaunching like this:

self->counter = [NSNumber numberWithFloat:0.0f];

Down the road I got thrown out with an Exception, checked my debugger and saw that counter was pointing to a <non objective c object>

I changed the line to :

self.counter = [NSNumber numberWithFloat:0.0f];

And now I see in the debugger that I have yes another variable.

So, what is happening here?

Was it helpful?

Solution

self->counter = [NSNumber numberWithFloat:0.0f]; uses direct access to an ivar. With self, it is equal to counter = [NSNumber numberWithFloat:0.0f]; where counter is an ivar. That is to say self-> is a redundant scope qualification within an instance method.

self.counter = [NSNumber numberWithFloat:0.0f]; is syntactic sugar for [self setCounter:[NSNumber numberWithFloat:0.0f]];. Specifically, the declaration dynamically messages the object's setter for counter. Although there are exceptions, you should favor using the accessor when not in a partially constructed/destructed state.

OTHER TIPS

You're asking about the difference between iVars and properties. There's already a great answer on this question here.

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