-1

I have been looking at some code and come across the symbol -> being used like obj->method(argument); I have done a little bit of research and found it basically is the same as [obj method:argument]; but I am unsure what -> actually is or does.

So my question is, what does the -> symbol mean in objective-c?

Popeye
  • 11,839
  • 9
  • 58
  • 91
  • 1
    @JordiKroon I would tend to disagree that these are duplicates. This is asking what -> symbol means, whilst that question is asking the difference between "." and "->" – Popeye Feb 19 '13 at 15:29
  • Same thing it represents in C, which you should learn before diving into Objective-C. – Hot Licks Feb 19 '13 at 16:55
  • 1
    @HotLicks I developed loads of apps that are in the app store and I haven't had to learn what this is. The only reason I ask is because for the first time in 3 years I have seen it being used in some open source code. So I wouldn't say it needs to be learnt before diving into objective-c – Popeye Feb 19 '13 at 16:58
  • @JoshCaswell Could possibly be the rude manner why someone has down voted? – Popeye Feb 19 '13 at 21:13

2 Answers2

6

It means the same as the struct dereference operator does in C, which is used to access fields within the struct via a pointer:

struct mystruct
{
    int field;
};

struct mystruct *mystruct = ...;
printf("field=%d\n", mystruct->field);

In Objective-C it can also be used to access fields within Objective-C objects:

@interface MyObj : NSObject
{
@public
    int field;
}
@end

MyObj *myObj = [[MyObj alloc] init];
NSLog(@"field=%d", myObj->field);

Note that you can only access these fields externally if they are declared @public.

trojanfoe
  • 120,358
  • 21
  • 212
  • 242
  • 1
    +1. Note that this second usage is not common (I've never personally used it in real shipping code), and that it's also actually the same exact thing as the first usage. Objective-C objects are implemented as structs internally, so a pointer to an object is actually a pointer to a struct. – Andrew Madsen Feb 19 '13 at 16:11
  • Sort of as structs anyway; they were straight structs up until the modern runtime. Now there is a bit of magic to address the fragile base class problem (which allows ivars to be added to a base class without requiring subclasses to be recompiled). – bbum Feb 19 '13 at 16:17
  • @bbum and Andrew; one thing you can clarify to me is the OP claims to be able to call methods via `->`, but how is `self` passed to the method in this case; does the compiler just treat it like an Objective-C call? – trojanfoe Feb 19 '13 at 16:21
  • 1
    Yeah -- no :) -- he can't call methods through that syntax. – bbum Feb 19 '13 at 16:49
  • Can't call *Objective-C* methods. Can call C++ methods inside Objective-C++ modules. – Hot Licks Feb 19 '13 at 17:33
  • I guess you can only do this using `methodForSelector` to get the method pointer and passing `self` and `_cmd` through explicitly? https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtHowMessagingWorks.html#//apple_ref/doc/uid/TP40008048-CH104-TPXREF163 – trojanfoe Feb 19 '13 at 17:58
5

I have been looking at some code and come across the symbol -> being used like obj->method(argument); I have done a little bit of research and found it basically is the same as [obj method:argument]; but I am unsure what -> actually is or does.

So my question is, what does the -> symbol mean in objective-c?

Exactly the same thing it means in C; it is for accessing an item in a C structure. Way back in the days of yore, Objective-C was implemented purely as a C preprocessor extension + a runtime. Classes were nothing more than concatenated C structures and the preprocessor turned each ivar access into self->ivar.

I.e. ivar and self->ivar do the same thing (in a method of class).

Now, you can use -> to poke at some other object's (@public) ivars. But don't. That breaks encapsulation exactly because Objective-C's line of encapsulation is drawn at the method interface. Always use the setters/getters such that behavior can be either observed or overridden.

Finally, no, there is nothing like obj->method(argument) anymore. There was, once, in a failed experiment called Modern Syntax, but it was abandoned because it was a pointless waste of time. You can't use -> to invoke methods.

bbum
  • 162,346
  • 23
  • 271
  • 359