I am mystified by ObjC's message syntax

I’ve done lots of C programming and OOP of various flavors (Java and Perl mostly) so when it comes to passing arguments, I’m used to sticking them in a parenthesized list after the function/method name.

This doesn’t appear to be what you do with Objective C method calls, though. Here’s an example from my book:

[rectangle setWidth:width]

This, apparently, calls the setWidth method on the object rectangle, and passes the value of width as the argument. Makes sense so far. But then they show me this:

[rectangle setWidth:width height:height]

This is where I get lost. Apparently, this method is called setWidth:height, yet the width argument is stuck right in the middle of the method name (WTF?). It feels like they’re trying to do named parameters in a syntax that wasn’t exactly designed for it, or something. I don’t understand where the method names are ending and the arguments are beginning. Why is it called setWidth if it takes both a width and a height? And what’s the syntax for a method with more than two arguments? (For example, something that takes a width, height and x,y offset). Unfortunately, the book does not go into more detail after this example.


Bu-bump beedump.

Yeah, this bugs me sometimes to. Try to think of it like this… you’ve got your object rectangle, which has a method called setWidth:. You’ve also got another method called setWidth: that exhibits a type of polymorphism or function overriding that allows you to also set the height.

In your class, you’re really defining two methods, setWidth: and setWidth:height. It’s a convenience when you start to implement. You can choose (dynamically, even) to use whichever method you want, using the same name.

The alternative is to always use the only a setWidth: and a separate setHeight: method. Objective-C being as flexible as it is, it would soon be a major pain in the butt to constantly refer to the same object over and over again to call multiple methods.

I guess I didn’t answer the rest of everything, though.

In your example, the syntax would be something like:

setWidth:height:xOffset:yOffset, and you’d call it like this:

[rectangle setWidth:w height:h xOffset:x yOffset:y]

Presumably, there’s be “overridden” methods like:


Essentially, you’re given the same group of “messages” that can be sent to the object at the same time, as long as you keep your naming consistent. In reality, though, they’re all distinct messages.

As to where method names and arguments begin and end, well, like in C, all methods take arguments, even if void. So the “method name” is really also the first argument. Then the colons spell out the rest of them. This is probably where you’re most confused, so I’ll say it again: the method name is really the name of the first argument.

Thanks, that’s beginning to make more sense to me. I really like Objective C so far, so I’m going to have to get used to this weirdness. Oh well.

Are you doing Cocoa or something on Linux?

Cocoa. I’m working through the examples in the Cocoa with Objective C book right now.