Mailing Lists: Apple Mailing Lists
Image of Mac OS face in stamp
Re: a question about "->"
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: a question about "->"



On 2 Jul 2010, at 09:59, Patrick J. Collins wrote:

> Hi everyone,
>
> I was looking over an article on properties:
> http://www.cocoacast.com/?q=node/103
> and I noticed the notation "->", which I am a bit unfamiliar with.  It looks as
> though it is a way to access the getter method from an object such as:
>
> instance->property_name
>
> Atleast, that's what I think is going on..  I have always done this as:
> [instance property_name]
>
> ..  However, I tried doing the -> way in a test command-line tool and when I
> did, I got a warning that an instance variable was protected and it will be a
> hard failure in the future..
>
> So obviously I am not understanding exactly what "->" is...  From that error,
> it seems like it's just a way to access an instance variable from within an
> class----- but... why would you need to do self->var_name ?  you should just be
> able to do var_name by itself.. Right?

the -> operator is the same operator as it is in C, it dereferences a pointer, and then accesses a value from a struct/union:

a->b; is equivalent to (*a).b;

In your case, it's not accessing the property (no setter or getter is called when using it), instead it's accessing the ivar directly. In the context of objective-c it's a good indication of at very least a style issue, and sometimes a bug.  You in general, should stick to using the property accessor methods, even when within your own class, that way, your abstraction barrier remains in place.

Thanks

Tom Davie _______________________________________________
Do not post admin requests to the list. They will be ignored.
Objc-language mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

References: 
 >a question about "->" (From: "Patrick J. Collins" <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2011 Apple Inc. All rights reserved.