First page Back Continue Last page Graphics

Infix Notation

Objective-C uses a syntax technique called infix notation. The name of the method and its arguments are all intertwined. For instance, you call a single-argument method like this:

[circle setFillColor: kRedColor];

A method that takes two arguments is called like this:

[textThing setStringValue: @"hello there"

color: kBlueColor];

The setStringValue: and color: thingies are the names of the arguments (and are actually part of the method name—more on that later), and @"hello there" and kBlueColor are the arguments being passed.

This syntax differs from C, in which you call a function with its name followed by all its arguments, like so:

setTextThingValueColor (textThing, @"hello there", kBlueColor);

We really like the infix syntax, although it does look a little weird at first. It makes the code very readable, and it’s easy to match arguments with what they do. With C and C++ code, you’ll sometimes have four or five arguments to a function, and knowing exactly which argument does what without consulting the documentation can be difficult.