Ted Ted - 2 years ago 103
iOS Question

what is the difference between '.' and '->' iOS?

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?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download