JAM JAM - 2 months ago 5x
Objective-C Question

If Protocol method is marked @required, when not implemented, why does compiler issue a warning and not an error?

Assume that:

  • New Protocol is declared

  • Method in this protocol is marked

  • Class conforms to Protocol

  • Class does not implement the method mentioned in Protocol

At compile time, information about this method is known: i.e. that it is required and that this class and any other classes this class may may extend do not implement it.

Why in this case the compiler issues a warning and not an error?


Errors are only issued when the compiler cannot continue because something went terribly wrong.

When calling a method in Objective-C, the method lookup is done during runtime and not during compilation, which C++ does. In Objective-C a "message" is simply sent to the object, something like obj.executeCommand("Hey, can you execute function <name> for me?"). In C++ the object will be called directly, in a way like obj.<name>(). In the case of Objective-C the executeCommand() method is called, which exists. In C++'s case the function is called but it does not exist. These are methods that are linked on the compiler level, which means they both become memory addresses rather than names. executeCommand becomes 0x12345678 but it still uses the same message ("execute function <name>").

This is probably very confusing, but it's related to the way methods are implemented in different languages.