In Objective-C, how do I test the object type?

Objective C

Objective C Problem Overview


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?

Objective C Solutions


Solution 1 - Objective C

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]]

Solution 2 - Objective C

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");	
}

Solution 3 - Objective C

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.

Solution 4 - Objective C

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.

Solution 5 - Objective C

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)) 

Solution 6 - Objective C

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

Solution 7 - Objective C

Swift 5

'is' is the equivalent of isKindOfClass in swift

Here, You can simply use,

myObject is NSString

myObject is UIImageView

More on the topic

class Animal {}
class Dog: Animal {}
class Cat: Animal {}

let c = Cat()
let myObject = Cat()

//isKindOfClass equivalent
c is Cat //true
c is Dog //false
c is Animal //true

//isMemberOfClass equivalent
type(of: c) == Cat.self //true
type(of: c) == Dog.self //false
type(of: c) == Animal.self //false

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJames SkidmoreView Question on Stackoverflow
Solution 1 - Objective CmmcView Answer on Stackoverflow
Solution 2 - Objective CBryan HareView Answer on Stackoverflow
Solution 3 - Objective CYedyView Answer on Stackoverflow
Solution 4 - Objective CAlex ZavatoneView Answer on Stackoverflow
Solution 5 - Objective CBajjuView Answer on Stackoverflow
Solution 6 - Objective CAMohanView Answer on Stackoverflow
Solution 7 - Objective CThilina Chamath HewagamaView Answer on Stackoverflow