How does Objective-C provide a "dynamic" runtime? What does "dynamic" refer to here?
In one sentence: Objective-C decides which method implementation to call right before doing so (during runtime). The idea is that the connection between the name of a method and the implementation is dynamic. C++ for example resolves names during compile time.
Example:
id object = @"1";
int i = [object intValue];
object = @1;
i = [object intValue];
In this example the intValue
message is first sent to an instance of NSString
and then to an NSNumber
. The code emitted by the compiler is identical for both calls–in fact the compiler does not even know to which kind of object it's sending messages to (as the type is id
).
The runtime decides which implementation to call to extract an int value from a string or an NSNumber
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With