views:

190

answers:

2

in Objective-c I have this function prototype: -(NSString*)formatSQL:(NSString*) sql, ... I may pass to this function any type of parameters: NSString, NSNumber, integer, float How can I determine in the function if a parameter is an object (NSString..) or a primitive (integer...)? thanks BrochPirate

+5  A: 

If you're going to have a parameter that accepts multiple types, you can only safely do it by using Obj-C objects, which means using id as the type. You can't safely inter-mingle id with float, integer etc.

If you wrapped up all floats and ints in NSNumbers, you could have a method like so:

- (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 using isKindOfClass:, but this should serve as a good starting point.

Nick Forge
A: 

Tried this. The thing is, if the parameter (say 'obj'), is a primitive (interger, float), the command: [obj isKindOfClass:... kills the app with "bad access" error. This error is not trapable by @try/@catch - otherwise this would have been a way to identify the type. thanks though, Broch

I understand that an NSNumber wrapper would circumvent the initial propblem, however I am trying to minimize the impact on other code pieces therefore woluld appriciate a way to distinguish between 'object' an 'primitive'.

It might help if I had a way to assert whether an object (actually a pointer) points to a valid address within my address space...

Thanks Broch

Broch Pirate
Notice Nick Forge's point about wrapping your primitives in objects of the NSNumber class. This exists for this precise purpose.
harms
It's not possible, in any way, to have a parameter that takes eith an `int`, a `float` or an Obj-C object. It's just not possible with the way that Obj-C is implemented. The only way that you can take multiple types in a parameter is if they are Obj-C objects, and you use `id`.Also, this "answer" should be a comment on my answer. You should put any further comments there.
Nick Forge
I'm guessing the original poster did that because he had only one point, and you need 10 to comment IIRC.
harms
hey harns,sorry. I'm new here. I don't understand your comment (I assume I am the 'original poster' but I am not sure what the rest means.Broch Pirate
Broch Pirate
Nick's point was that you should add your answer (the one that begins with "Tried this") as a comment to his answer instead, since your answer is not really an answer but a follow-up. Stack Overflow makes a strict distinction between answers and comments.
harms
Thanks.I understand. Will do in future comments.
Broch Pirate
BTW, even if an answer tells you that what you want is impossible (which is the case here), you probably should mark it as the "accepted" answer. ;-)
Nick Forge
Ok. Sure.How do I do it? I don't see an 'accepted' button. Am I missing something?
Broch Pirate
You seem to have created two different accounts! :-) http://stackoverflow.com/users/328737/broch-pirate http://stackoverflow.com/users/328548/broch-pirate The second one is the one you asked the question in, so you can only "accept" the answer through that account.
Nick Forge