Dynamic Binding and the id Type

Chapter 4, “Data Types and Expressions,” briefly touched on the id data type and noted that it is a generic object type. That is, id can be used for storing objects that belong to any class. The real power of this data type is exploited when it’s used this way to store different types of objects in a variable during the execution of a program. Study Program 9.2 and its associated output.

Program 9.2 Test Program main.m

// Illustrate Dynamic Typing and Binding#import "Fraction.h"#import "Complex.h"int main (int argc, char * argv[]){   @autoreleasepool {      id    dataValue;      Fraction *f1 = [[Fraction alloc] init];      Complex  *c1 = [[Complex alloc] init];      [f1 setTo: 2 ...

Get Programming in Objective-C, Sixth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.