Reputation: 303
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?
Upvotes: 1
Views: 353
Reputation: 100622
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.
Upvotes: 1