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

后端 未结 5 485
没有蜡笔的小新
没有蜡笔的小新 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 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.

提交回复
热议问题