Compile Time Versus Runtime Checking

Because the type of object stored inside an id variable can be indeterminate at compile time, some tests are deferred until runtime—that is, while the program is executing.

Consider the following sequence of code:

Fraction *f1 = [[Fraction alloc] init];[f1 setReal: 10.0 andImaginary: 2.5];

Recalling that the setReal:andImaginary: method applies to complex numbers and not fractions, the following message is issued when you compile the program containing this line:

'Fraction' may not respond to 'setReal:andImaginary:'

The Objective-C compiler knows that f1 is a Fraction object because it has been declared that way. It also knows that when it sees the ...

Get Programming in Objective-C, Sixth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.