Question

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 :)

Was it helpful?

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;
        ...
    }
 }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top