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?

- 14,578
- 4
- 19
- 47

- 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
-
36Yes there is: `[object isKindOfClass:[ClassName class]]` – Philippe Leybaert Jul 17 '09 at 17:28
6 Answers
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]]

- 20,427
- 11
- 57
- 70

- 17,354
- 2
- 34
- 52
-
93Note 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.) – Craig Otis Jul 17 '09 at 17:45
-
4
-
Is that right knowing that NSString is a class cluster? (like NSNumber) – Ricardo Nov 24 '15 at 10:45
-
but this answer isnt in the form of a test, like with kiwi or something – CDM social medias in bio Jul 22 '21 at 13:13
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");
}

- 7,839
- 2
- 19
- 35

- 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
-
6For 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
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.

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

- 4,106
- 36
- 54
-
1
-
The idea is for you to copy my code and see the results for what you need. – Alex Zavatone Oct 15 '18 at 00:09
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))

- 3,895
- 4
- 45
- 61

- 925
- 11
- 27
Simple, [yourobject class] it will return the class name of yourobject.

- 534
- 1
- 6
- 15
-
2Actually 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