583

I need to test whether the object is of type NSString or UIImageView. How can I accomplish this? Is there some type of "isoftype" method?

2

6 Answers 6

918

If your object is myObject, and you want to test to see if it is an NSString, the code would be:

[myObject isKindOfClass:[NSString class]] 

Likewise, if you wanted to test myObject for a UIImageView:

[myObject isKindOfClass:[UIImageView class]] 
Sign up to request clarification or add additional context in comments.

4 Comments

Note that there is also a isMemberOfClass: method that will check for class "exactness." Be careful with it though, as many Apple objects are actually Core Foundation types in disguise. (Eg. an NSString is more often an NSCFString, and isMemberOfClass: will return false for this comparison.)
Note this method is very slow!
Is that right knowing that NSString is a class cluster? (like NSNumber)
but this answer isnt in the form of a test, like with kiwi or something
55

You would probably use

- (BOOL)isKindOfClass:(Class)aClass 

This is a method of NSObject.

For more info check the NSObject documentation.

This is how you use this.

BOOL test = [self isKindOfClass:[SomeClass class]]; 

You might also try doing somthing like this

for(id element in myArray) { NSLog(@"======================================="); NSLog(@"Is of type: %@", [element className]); NSLog(@"Is of type NSString?: %@", ([[element className] isMemberOfClass:[NSString class]])? @"Yes" : @"No"); NSLog(@"Is a kind of NSString: %@", ([[element classForCoder] isSubclassOfClass:[NSString class]])? @"Yes" : @"No"); } 

3 Comments

-1: Don't use className, it may or may not work on OS X, but it's not in iOS and it's not meant to be used for this. Also, those NSLogs don't do what I think you meant them to--they are checking the class name (always a string) and the coder class, strangely, instead of the class of the actual element.
For a quick check this: NSLog(@"Is of type: %@", [element class]); will work in iOS 7
@PortlandRunner : This is exactly what I was looking for. IDK if there is a ticket that addresses that exact thing better, but if not, you should create one. If you do, I let me know and I will upvote the answer there, as this snippet was clutch for me. Thanks again.
16

When you want to differ between a superClass and the inheritedClass you can use:

if([myTestClass class] == [myInheritedClass class]){ NSLog(@"I'm the inheritedClass); } if([myTestClass class] == [mySuperClass class]){ NSLog(@"I'm the superClass); } 

Using - (BOOL)isKindOfClass:(Class)aClass in this case would result in TRUE both times because the inheritedClass is also a kind of the superClass.

1 Comment

isMemberOfClass: will return NO when dealing with subclasses.
14

Running a simple test, I thought I'd document what works and what doesn't. Often I see people checking to see if the object's class is a member of the other class or is equal to the other class.

For the line below, we have some poorly formed data that can be an NSArray, an NSDictionary or (null).

NSArray *hits = [[[myXML objectForKey: @"Answer"] objectForKey: @"hits"] objectForKey: @"Hit"]; 

These are the tests that were performed:

NSLog(@"%@", [hits class]); if ([hits isMemberOfClass:[NSMutableArray class]]) { NSLog(@"%@", [hits class]); } if ([hits isMemberOfClass:[NSMutableDictionary class]]) { NSLog(@"%@", [hits class]); } if ([hits isMemberOfClass:[NSArray class]]) { NSLog(@"%@", [hits class]); } if ([hits isMemberOfClass:[NSDictionary class]]) { NSLog(@"%@", [hits class]); } if ([hits isKindOfClass:[NSMutableDictionary class]]) { NSLog(@"%@", [hits class]); } if ([hits isKindOfClass:[NSDictionary class]]) { NSLog(@"%@", [hits class]); } if ([hits isKindOfClass:[NSArray class]]) { NSLog(@"%@", [hits class]); } if ([hits isKindOfClass:[NSMutableArray class]]) { NSLog(@"%@", [hits class]); } 

isKindOfClass worked rather well while isMemberOfClass didn't.

2 Comments

I see you shared code for tests. But where are the printed result?
The idea is for you to copy my code and see the results for what you need.
11

You can make use of the following code incase you want to check the types of primitive data types.

// Returns 0 if the object type is equal to double strcmp([myNumber objCType], @encode(double)) 

Comments

6

Simple, [yourobject class] it will return the class name of yourobject.

1 Comment

Actually it will return a Class object. However, the description of this object will be the class name as a string, so you can therefore still log it to the console.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.