Objective-c determine if parameter is an object
in Objective-c I have this function prototype: - (NSString *) formatSQL: (NSString *) sql, ... I can pass any type of parameters to this function: NSString, NSNumber, integer, float How to define in a function if the parameter is it an object (NSString ..) or primitive (integer ...)? thanks to BrochPirate
a source to share
If you have a parameter that accepts multiple types, you can only safely do so using Obj-C objects, which means using id
as a type. You cannot safely mix id
with float
, integer
etc.
If you are all wrapped float
and int
in NSNumber
s, you could be this way:
- (NSString *)formatSQL:(id)obj
{
if ([obj isKindOfClass:[NSString class]]) {
// Format as a string
}
else if ([obj isKindOfClass:[NSNumber class]]) {
// Further processing will be required to differentiate between ints and floats
}
}
There are a few caveats to use isKindOfClass:
, but this should serve as a good starting point.
a source to share