Lets say I have made a Fraction class in Objective-C (as in the book "Programming with Objective-C"). One of the methods, add:, was first created like so:
//Fraction.h & most of Fraction.m left out for brevity's sake.
-(Fraction *)add: (Fraction*) f {
Fraction *result = [[Fraction alloc] init];
//Notice the dot-notation for the f-Fraction
result.numerator = numerator * f.denominator + denominator * f.numerator;
result.denominator = denominator * f.denominator;
return result;
}
Then in one of the later exercises, it says change the return type and argument type to id & make it work. The dot-notation, as shown above doesn't work any more, so I changed it to this:
-(id)add: (id)f {
Fraction *result = [[Fraction alloc] init];
result.numerator = numerator * [f denominator] + denominator * [f numerator];
// So forth and so on...
return result;
}
Now my guess to why the dot-notation needs to be changed is because up until run-time, the program doesn't know what type of object that was passed to the add argument(f), therefore the compiler doesn't know of any accessor methods for f.
Am I anywhere close to understanding this? If not, can someone please clarify?