Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When/where the arrow notation "->" should be used in Objective-C?

I've just got the clear explanation of what "->" notation is about here: Dot (".") operator and arrow ("->") operator use in C vs. Objective-C

But I still don't understand what are really the use cases of this notation in Objective-C?

Here is the example of what I'm talking about: https://github.com/gnustep/gnustep-base/blob/master/Source/NSOperation.m - why all these strings like internal->lock are written there - why not just use ivars or dot-notation?


Related topic: Performance of object_setClass() instead of assigning isa pointer.

like image 949
Stanislav Pankevich Avatar asked Mar 15 '13 18:03

Stanislav Pankevich


People also ask

What is the use of -> operator in C?

In C, this operator enables the programmer to access the data elements of a Structure or a Union. This operator (->) is built using a minus(-) operator and a greater than(>) relational operator. Moreover, it helps us access the members of the struct or union that a pointer variable refers to.

What does arrow mean in Objective C?

Arrow operator on an Objective-C object (pointer) (at run time) Dereference pointer. Return value of the field.

What does arrow mean in C programming?

Master C and Embedded C Programming- Learn as you go (dot) operator and the -> (arrow) operator are used to reference individual members of classes, structures, and unions. The dot operator is applied to the actual object. The arrow operator is used with a pointer to an object.

What is the difference between accessing elements of a structure using arrow notation and dot notation?

The dot ( . ) operator is used to access a member of a struct, while the arrow operator ( -> ) in C is used to access a member of a struct which is referenced by the pointer in question.


1 Answers

From your question, it's not clear if you understand what the -> operator does.

That example in the GNUStep NSOperation source is using an ivar. That's what the -> operator does — it dereferences the pointer and accesses the named member.

As for "Why not use dot notation?" The obvious answer would be that they didn't want to go through an accessor. Going through an accessor is slower than direct access and has no real benefit in a case like this where we're just working with "dumb" internal state.

So when should you use it in your Objective-C code? Mainly when you're accessing a struct through a pointer. There is seldom a need to access another object's instance variables directly. If you do, that code is the exception, not the rule.

like image 115
Chuck Avatar answered Oct 21 '22 04:10

Chuck