AAV AAV - 5 days ago 4
Objective-C Question

What is difference between dynamic binding Vs dynamic typing in Objective C

I am having hard time to understand what is difference between dynamic binding Vs dynamic typing in Objective C. Can someone explain this ?

Answer

Dynamic typing in Objective-C means that the class of an object of type id is unknown at compile time, and instead is discovered at runtime when a message is sent to the object. For example, in the following code, the class of foo isn't known until we attempt to send the message componentsSeparatedByString:.

id foo = @"One Two Three";
NSArray *a = [foo componentsSeparatedByString:@" "];

If instead of using the id data type we had done the following...

NSString *foo = @"One Two Three";

...then we'd be using static typing rather than dynamic typing.

Dynamic binding means that the compiler doesn't know which method implementation will be selected; instead the method implementation is looked up at runtime when the message is sent. It basically helps us with Polymorphism. So

[foo description]

results in invoking a different method implementation if, for example, foo is an instance of NSArray rather than an instance of NSString.

Comments