Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Objective-C provide a dynamic runtime?

How does Objective-C provide a "dynamic" runtime? What does "dynamic" refer to here?

like image 659
Coder Avatar asked Nov 09 '12 10:11

Coder


1 Answers

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.

like image 161
Nikolai Ruhe Avatar answered Oct 17 '22 20:10

Nikolai Ruhe