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

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.

Leave a Comment