views:

37

answers:

2

For example if I have a function sort() like so:

+ (void) sort: (id) a {
 if(typeof(a) == 'NSArray')
  { ... }
}

So is there anything in Objective C for Iphone which can go in place of typeof() so that I can detect beforehand what kind of variable am I dealing with?

+2  A: 
[a isKindOfClass:[NSArray class]]

Springs to mind.

I do want to point out though that in your case it makes more sense to simply type the method argument, rather than taking id and checking it's type, i.e.

+ (void)sort:(NSArray *)a
dannywartnaby
Thanks for the tip. I intend to pass it objects of various kinds, eg, NSArray, NSMutableArray, NSDictionary, NSMutableDictionary and so on and do operations based on the type.
kumar
That makes sense, but be warned it's often a symptom of a weak design.
Mike Abdullah
+1  A: 

The NSObject Protocol has comparison methods that you will be interested in.

Jacob Relkin