11.9 The id type

The id type is special in Objective-C/Pascal. It is much like the pointer type in Object Pascal, except that it is a real class. It is assignment-compatible with instances of every objcclass and objcprotocol type, in two directions:

  1. variables of any objcclass/objcprotocol type can be assigned to a variable of the type id.

  2. variables of type id can be assigned to variables of any particular objcclass/objcprotocol type.

No explicit typecast is required for either of these assignments.

Additionally, any Objective-C method declared in an objcclass or objccategory that is in scope can be called when using an id-typed variable.

If, at run time, the actual objcclass instance stored in the id-typed variable does not respond to the sent message, the program will terminate with a run time error: much like the dispatch mechanism for variants under MS-Windows.

When there are multiple methods with the same Pascal identifier, the compiler will use the standard overload resolution logic to pick the most appropriate method. In this process, it will behave as if all objcclass/objccategory methods in scope have been declared as global procedures/functions with the overload specifier. Likewise, the compiler will print an error if it cannot determine which overloaded method to call.

In such cases, a list of all methods that could be used to implement the call will be printed as a hint.

To resolve the error, an explicit type cast must be used to tell the compiler which objcclass type contains the needed method.