简体   繁体   中英

Struct isKindOfClass

How do I find out if a struct is of a specific type? In other words, if I get an object, how do I know that the underluying type is a struct?

+(BOOL)isPrimitive:(id)input
{
    return [input isKindOfClass:[NSNumber class] ] || [input isKindOfClass:[NSDate class]] || [input isKindOfClass:[NSString class]]
        ||  __IS_THIS_A_STRUCT__ (specifically SEL);
}

What should I put in place of IS_THIS_A_STRUCT ?

Based on your comments, it looks like you know a property and want to act in a certain way if it returns a struct. If so then you could do something like:

if(!strcmp([[self class] 
    instanceMethodSignatureForSelector:@selector(propertyName)].methodReturnType, 
    @encode(SEL)))

@encode returns the type encoding for the named type, which is a C string. instanceMethodSignatureForSelector returns an NSMethodSignature which can nominate the return type of that method as an encoded type.

The two type encodings are not guaranteed to have the same identity but will have the same value. So you can use the C function strcmp to check that they're the same.

You can use NSSelectorFromString if the selector name is not known at compile time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM