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

2

8

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.

Filter answered 15/3, 2013 at 18:26 Comment(6)
That answer containes everything, what else you want to know>Calyx
@AnoopVaidya, the quoted article does not describe the use-cases and particularly does not describe why one notation should be preferred over the other. I think, I did ask the meaningful question here.Filter
My guess is authors did not want to use dot notation to avoid side-effects of calling accessor methods + they wanted to be able to redefine 'internal' pointer to whatever they need in future, so -> provides exactly that flexibilityXerxes
@Vladimir, sorry, I don't understand your "side-effects of calling accessor methods" and "to be able to redefine 'internal' pointer to whatever they need in future" quite well. Could you please format these your points in an answer? I will be very thankful!Filter
Those internals are a bunch of complicate macros to implement non-fragile stuffs for different compilers and configurations. I think you better look at something else. In fact, GNUstep source code is a bad place to start learning things, they pulled all their tricks to optimize things since the days the computers were slow.Balcom
Thanks, @FredFrith-MacDonald, for your additional clarification.Filter
H
7

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.

Hamza answered 15/3, 2013 at 19:3 Comment(4)
Thanks for the answer. Pretty clear. "...If you do, that code is the exception, not the rule.". Could you explain, in what kind of situations this might be needed? (I especially wonder about it because I myself rarely need to access ivars directly, also see fx how "Github Objective-C conventions" recommend: Don't access an ivar unless you're in -init or -dealloc and so on)Filter
@Stanislaw: I really don't know if I can explain where this would be needed much more precisely than "exceptional situations." I'm kind of tempted to say "Situations where you need to eke out every last bit of performance and don't want the overhead of message sends," but in even those situations there are often better design-level optimizations you could do that would obviate the need. But that's the kind of situation where it will often come up.Hamza
I read your comment as that I need to use -> "almost never" ))). Thanks.Filter
You can often use it in class method to initialize allocated instances at faster speed ie. without calling -init, especially since class can access private properties of its instances.Balcom
R
1

May be in this case.I saw an example in apple's Reachability.m file.Here it is:

@implementation Reachability
{
    SCNetworkReachabilityRef _reachabilityRef;
}

+ (instancetype)reachabilityWithHostName:(NSString *)hostName
{
    ...
    if (reachability != NULL)
    {
        returnValue= [[self alloc] init];
        if (returnValue != NULL)
        {
            returnValue->_reachabilityRef = reachability;
        }
        ...
    }
    return returnValue;
}

So you can use it to call an global variable by an object in a class method.

Reinold answered 26/10, 2017 at 5:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.