How does the compiler know the object type is
UIDevice
?
It doesn't.
It sees that it's id
. The type id
is the generic object type. Thus, the compiler accepts without an error if you send it any message. Even if you send a nonexistent one.
This is because Objective-C is a dynamic language. Method calls (message sends), binding, even types are inferred during runtime, by the Objective-C runtime library. If you send an object a message it doesn't implement ("respond to"), then instead of a compiler error, a runtime exception will be thrown.
By the way, messages to id
are assumed to return either id
(so at most pointer-sized values, such as most integers may safely be returned), or the compiler looks at all the available selector names on all classes and tries to match the type to one of the selectors it found in case it was indeed found.