1

Possible Duplicate:
Arrow operator (->) usage in C
Dot (“.”) operator and arrow (“->”) operator use in C vs. Objective-C

I'm a newbie looking at a freeware/open-source program last updated in 2008, and I don't recognize the -> in the following notation:

- (id)copyWithZone:(NSZone *)zone
{
  GFIPGeniusItem * newItem = [[[self class] allocWithZone:zone] init];
  newItem->_stringValue = [_stringValue copy];
  newItem->_imageURL = [_imageURL copy];
  newItem->_webResourceURL = [_webResourceURL copy];
  newItem->_speakableStringValue = [_speakableStringValue copy];
  newItem->_soundURL = [_soundURL copy];
  return newItem;
}

I'm assuming it's allowing some sort of shortcut, but I'd love to specifically what it does.

Community
  • 1
  • 1
Joel Derfner
  • 2,207
  • 6
  • 33
  • 45

4 Answers4

6

It's a way to directly access an instance variable within an ObjC object from outside that object. (The syntax and -> is borrowed from C structs, behaving as if the reference were a pointer-to-structure).

This access mechanism is almost vestigial at this point, and very uncommonly seen in modern ObjC code, because good encapsulation requires the use of accessors and properties, not touching instance variables directly. It's legitimate in some very special cases, though, and this is one of them:

When copying an object, you want to get a resulting copy that matches exactly the state of the current self. The cleanest way of achieving this is often to set the copy's ivars explicitly, to prevent any side-effects that the init overloads or accessors might cause. It's "safe" because the code doing it is still located within the class that's in question, so if you needed to change or add ivars, you could update this code as well with the knowledge of anything else that might require.

Doing this from outside the class in question is bad form, and there's no good reason to do it.

Ben Zotto
  • 70,108
  • 23
  • 141
  • 204
  • Well, it's still seen in modern ObjC code at the very bottom level. But unless you're, e.g., porting Cocoa or gnustep, it's very rare that you'll need to write, or even read, the code at that level. (That being said, I did use this just a couple years ago, building custom DO-style proxies that had to work the same way with both the classic and modern runtimes.) – abarnert Sep 24 '12 at 22:04
2

That's a pointer indirection operator. a->b means the same thing as (*a).b (where the . is the structure member access operator, not Objective-C's property dot syntax).

When you say:

newItem->_stringValue

you're directly accessing the _stringValue instance variable of the object to which newItem points.

The -> operator is very common in C++, but not so much in Objective-C.

Caleb
  • 124,013
  • 19
  • 183
  • 272
2

In Objective C, like in C++, the p->m notation is equivalent to (*p).m This is, the dereference of the pointer to the base type followed by a call to the corresponding method or property.

So in your case, using the other notation it would look like this:

(*newItem)._stringValue = [_stringValue copy];

(It's more common to use the -> operator)

Jhovanny
  • 139
  • 2
  • 3
    However, the other notation is invalid in objc, as you cannot dereference an object. I am surprised that none of these answers bring that up. – Richard J. Ross III Sep 24 '12 at 21:59
  • 1
    Actually, is that still true? From what I can tell, both gcc and clang let you dereference an object pointer. You can't store or pass the result in ObjC, but in ObjC++ you can stick it in a `Foo&` (or `auto`). `(*f).x` seems to do the exact same thing as `f->x`, and in C++ if you do `Foo& g=*f` it does the same thing if you `g.x`. This may confuse ARC (haven't tested), but otherwise it seems to work. Of course it may be illegal even though it happens to work in both of Apple's implementations, but it makes me curious enough to raise the question of where the language says it's invalid. – abarnert Sep 24 '12 at 22:40
2

In Objective-C you have some kind of two variable type accessors. The one everybody should know is the "." one (e.g. Class.variable). This type calls either the appropriate getter or setter.

Now, the other type - the one you asked for - is for in-class usage. Obviously, as the getter or setter gets called automatically with the "." notation you need a way to set the variable without a setter (calling the setter in the setter itself results in an endless loop). Therefore, this "->" notation is used -> simply, it is the direct-access mode.

Usually, Objective-C the variable name for both notations is the same but some prefer to have the in-class notation variable name beginning with "_". This is achieved by editing the @synthesize variable line to @synthesize variable = _variable.