Search code examples
xcodeobjectparametersuniversal

Is it possible to create an action that can receive different types of parameters? (Xcode)


I was woundering if it is possible to make an action that can receive different types of objects. For example if I want to pass UILabels and UIButtons, to the same action, but only one at the time. What I'm not looking for is something like this:

- (void)actionInitWithButton:(UIButton *)button Label:(UILabel *)label;

More something like this:

- (void)actionInitWithObject:(UniversalObject *)object;

Thanks in advance :)


Solution

  • In Objective-C, they're simply called id. It's a principal concept of the language.

    The alternative is NSObject, but this is typically used for subclassing -- id covers a more broad spectrum as not all (but most) objects inherit from NSObject.

    Ex.

    - (void)actionInitWithObject:(id)something
    {
        if([something isKindOfClass:[UIButton class])
        {
            UIButton *button = (UIButton *)something;
            ...
        } else if ([something isKindOfClass:[UILabel class]) {
            UILabel *label = (UILabel *)something;
            ...
        }
     }