kumar
kumar

Reputation: 892

How to know the type of a variable in Objective C on Iphone?

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?

Upvotes: 1

Views: 898

Answers (2)

dannywartnaby
dannywartnaby

Reputation: 5542

[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

Upvotes: 3

Jacob Relkin
Jacob Relkin

Reputation: 163238

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

Upvotes: 1

Related Questions