577

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?

Willeke
  • 14,578
  • 4
  • 19
  • 47
James Skidmore
  • 49,340
  • 32
  • 108
  • 136
  • See: [In Objective-C what is the equivelant of the java “instanceof” keyword?](http://stackoverflow.com/questions/536396/in-objective-c-what-is-the-equivelant-of-the-java-instanceof-keyword) – Reunanen Jul 17 '09 at 17:28
  • 36
    Yes there is: `[object isKindOfClass:[ClassName class]]` – Philippe Leybaert Jul 17 '09 at 17:28

6 Answers6

912

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]]
Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
mmc
  • 17,354
  • 2
  • 34
  • 52
54

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");    
}
return true
  • 7,839
  • 2
  • 19
  • 35
Bryan Hare
  • 637
  • 6
  • 9
  • 5
    -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 `NSLog`s 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. – andyvn22 Aug 20 '13 at 21:21
  • 6
    For a quick check this: `NSLog(@"Is of type: %@", [element class]);` will work in iOS 7 – Automate This May 18 '14 at 22:46
  • 2
    @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. – Matt Williams Oct 22 '15 at 16:44
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.

Yedy
  • 2,107
  • 1
  • 25
  • 30
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.

Alex Zavatone
  • 4,106
  • 36
  • 54
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)) 
James Zaghini
  • 3,895
  • 4
  • 45
  • 61
Bajju
  • 925
  • 11
  • 27
6

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

AMohan
  • 534
  • 1
  • 6
  • 15
  • 2
    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. – devios1 Jun 09 '15 at 21:22