What's the difference between dot syntax and square bracket syntax?

后端 未结 5 476
没有蜡笔的小新
没有蜡笔的小新 2020-11-27 06:27

I am going through some walkthroughs fpr Objective-C and I got to many places where I raised my eyebrows. I would love to get them down.

  1. Is there a fundamen

相关标签:
5条回答
  • 2020-11-27 06:45

    It's a distinction oriented at the person reading your code. Dot syntax indicates state (I'm accessing an ivar), method syntax indicates behavior (I'm performing some action). To the runtime, both are the same.

    I think Apple's intention is to show accessors as an implementation detail you shouldn't worry about. Even when they could trigger side effects (due to some additional code in the accessor), they usually don't, so the abstraction is imperfect but worth it (IMHO). Another downside of using dot notation is that you don't really know if there is a struct or a union behind it (which unlike message sending, never trigger side effects when being assigned). Maybe Apple should have used something different from a dot. *shrugs*

    I think maybe nested messages cannot be created using the dot operator strategy?

    Dot notation can be used to nest calls, but consider the following:

    shu.phyl.we.spaj.da
    [[[[[shu]phyl]we]spaj]da]
    

    In this case, the uglier, the better. Both are a code smell because one object is creating dependencies to another object far far away, but if use brackets to pass messages you get that extra horrible syntax from the second line, which makes the code smell easier to notice. Again, convention is to use dots for properties and brackets for methods.

    0 讨论(0)
  • 2020-11-27 06:58

    1) They're both message sending, just different syntax. [object message] is traditional syntax, object.message is the "dot notation", but means exactly the same thing. You can do some kinds of nesting with dot notation, but you can't do anything with methods that take complex arguments. In general, old hand Obj-C programmers don't use dot notation except for simple accessor calls. IMHO.

    2) The runtime is really smart and can figure it out on the fly. The type casting of pointers is really just a clue to the compiler to let you know when you messed up. It doesn't mean a thing (in this case) when the message is sent to the array to fetch a value.

    0 讨论(0)
  • 2020-11-27 07:00
    1. Message sending is the preferred way of doing this. It's what the community uses and reinforces the concept of objects sending messages to one another which comes into play later when you get into working with selectors and asking an object if it responds to a selector (message).

    2. id is basically a pointer to anything. It takes some getting used to but it's the basis for how Objective-C handles dynamic typing of objects. When NSLog() comes across the %@ format specifier, it sends a description message to the object that should should replace the token (This is implemented in the superclass NSObject and can be overridden in the subclass to get the desired output).

    In the future when you're doing this, you might find it easier to do something like this instead:

    for (NSString *s in YourNSArrayInstance) //assuming they are NSStrings as you described
    {
        NSLog(@"%@", s);
    }
    

    Or even simply just:

    for (NSString *s in YourNSArrayInstance) //assuming they are NSStrings as you described
        NSLog(@"%@", s);
    

    You'll learn to like message sending eventually.

    0 讨论(0)
  • 2020-11-27 07:01

    1: Your terminology is incorrect. The dot operator is not "method calling", a different operation. It's just a different visual appearance for message sending. There's no difference between [x y] and x.y. The dot syntax can only take one argument though, as it's intended to be used only for property access.

    2: The static (compile-time) type of an object has no effect on its behavior at runtime. Your object is still an NSEnumerator even if you're calling it something else.

    0 讨论(0)
  • 2020-11-27 07:07

    I'm not sure what kind of distinction you're trying to make between "message sending" and "method calling", since they're two ways of describing the same thing. The dot syntax is just a shortcut for calling getters and setters, that is:

    [foo length]
    foo.length
    

    are exactly the same, as are:

    [foo setLength:5]
    foo.length = 5
    

    You should generally only use the dot syntax when you're using getters and setters; use the square bracket syntax for all of your other method calls.

    For your second question: this is how dynamic typing works. Any type declarations you put in your code are hints to the compiler; your Objective-C method calls will always work as long as the objects respond to them.

    0 讨论(0)
提交回复
热议问题