I created 2 objects subclass from NSObject
AObject, BObject
Is it possible to transfer variables of these 2 objects to a same function?
such as
-(void)myFunction:(AObject *)obj;
-(void)myFunction:(BObject *)obj;
I tested in xcode, it is not allowed.
Is there any replacement method?
Welcome any comment
Thanks
interdev
There are several options. Here are four that come to mind, each with their own pros and cons:
Discriminate by signature:
Declare a parameter of type
NSObject *(orid, as suggested in the comments) and query the type inside the function.BaseObject *, from whichAObjectandBObjectinherit.Combine the base class with a trampoline technique:
BaseObjectdeclares the abstract methodmyTrampoline:, whichAObjectandBObjectimplement.Without knowing more about your problem, it’s impossible to say which is best.