Uriel Griffin
Uriel Griffin

Reputation: 83

Declare a typedef with two types

What I need is to declate a C function that takes in both an Objective-C 'id' type and 'Class' types as a parameter, but nothing else.

Consider:

void the_function(classorid theargument);

I thought of declaring a typedef to achieve this purpose... something that could either hold an 'id' pointer or a 'Class' value... I would then figure out what value was what with my C function.

So... is there any way (or type?) I can use that lets me achieve this goal?

Thanks. :)

Upvotes: 2

Views: 325

Answers (2)

jscs
jscs

Reputation: 64002

Since classes are themselves represented by objects in ObjC, and id is the "generic object" type, you can in fact use id as the type for a pointer to a class. Defining your function with an id parameter will allow you to pass in both instances and Class objects. What you do with them inside the function (how you're going to distinguish them) is your business.

static void doThatThingYouDo(id obj) {

    NSLog(@"%@", obj);
}

int main(int argc, const char * argv[])
{

    @autoreleasepool {

        Class c = [NSString class];
        NSString * s = @"Rigatoni";

        id ci = c;

        doThatThingYouDo(c);
        doThatThingYouDo(s);


    }
    return 0;
}

Upvotes: 1

Related Questions