I'm pretty new to Objective C.
Let's assume there are 3 class: Chef, Food, Dishs
As you may expect: A Chef cook Food -> Dishe.
- (Dishe *)cook:(Food *)food;
Now i want to add ChefFish, Fish and FishDishe class.
As you may expect: When Chef cook fish, we will have FishDishe
Naturally i would go for:
- (FishDishe *)fishTraitment:(Fish *)fish {
return [FishDishe alloc];
}
- (Dishe *)cook:(Food *)food {
if ([food isKindOfClass:[Fish class]]) {
return [self fishTraitement:food];
} else {
return [Dishe alloc];
}
}
then i got this warning:
Incompatible pointer types sending 'Food *' to parameter of type 'Fish *'
Of course, the code itself compile and run as expected.
The warning is not the real matter, as we can avoid it with casting or simply move the fishTraitment block in cook method.
But searching around, i find some topic saying using isKindOfClass is not 'clean'.
It would violate polymorphism and object orientation principles.
My question here:
What would be the best practice here ?
Note that i could have elsewhere, an array of Chef that with generic Chef and ChefFish.
I would expect:
Chef cook fish -> Dishe
ChefFish cook fish -> FishDishe
Have also though about overloading, looks like we can't have method with same name & same number of parameters (event with different type & name), which is possible in Swift :(