How do I test whether an object is an instance of a particular class in Objective-C? Let's say I want to see if object a is an instance of class b, or class c, how do I go about doing it?
6 Answers
To test if object is an instance of class a:
[yourObject isKindOfClass:[a class]]
// Returns a Boolean value that indicates whether the receiver is an instance of
// given class or an instance of any class that inherits from that class.
or
[yourObject isMemberOfClass:[a class]]
// Returns a Boolean value that indicates whether the receiver is an instance of a
// given class.
To get object's class name you can use NSStringFromClass
function:
NSString *className = NSStringFromClass([yourObject class]);
or c-function from objective-c runtime api:
#import <objc/runtime.h>
/* ... */
const char* className = class_getName([yourObject class]);
NSLog(@"yourObject is a: %s", className);
EDIT: In Swift
if touch.view is UIPickerView {
// touch.view is of type UIPickerView
}

- 5,271
- 5
- 34
- 62

- 170,431
- 36
- 387
- 313
-
70don't forget to #import
for class_getName() – Ovesh Feb 02 '11 at 12:32 -
6There is also [a class method](http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSObject_Class/Reference/Reference.html) for this. – Alexander Wallin Feb 08 '11 at 16:53
-
3My understanding is that this only works for objects that inherit NSObject. – Henrik P. Hessel Jul 09 '11 at 19:39
-
@Henrik P.: or objects that conform to the NSObject protocol – user102008 Jul 22 '11 at 00:52
-
1@afEkenholm - but not on iOS 4.x, just so we know. – JJ Rohrer Oct 07 '11 at 14:40
-
1The class method specified by afEkenholm is only for mac apps, not [ios](http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSObject_Class/Reference/Reference.html) – qix Aug 13 '12 at 05:59
-
@Vladimir - is there a way to get the class name as a NSString directly? – Aug 16 '12 at 09:47
-
@tea, yes. see Henrik's answer – Vladimir Aug 16 '12 at 09:51
-
@Vladimir oh thanks - sorry didn't spot this one. My problem was that conversion to NString required explicit encoding and I'm not quite sure how this is defined in this case. – Aug 17 '12 at 10:01
-
1@tea, `NSStringFromClass([yourObject class])` can get the name of a class as an NSString. – William Denniss Aug 19 '13 at 14:17
-
2What is the advantage to using `class_getName` over `NSStringFromClass`? If there is none, this answer should be adjusted. – Dan Rosenstark Aug 20 '13 at 01:04
-
@Yar class_getName maybe 10 times faster then NSStringFromClass! And with NSStringFromClass using more memory space. and one more thing class_getName a bit slower then isKindOfClass. And I recommend not use so often constructions with isKindOfClass or class_getName. Will be better to have your own type, it's faster for your app. – Volodymyr B. Aug 29 '13 at 10:36
-
@SAKrisT and does that 10x faster diminish if I turn the `char*` into an `NSString` right after? – Dan Rosenstark Sep 06 '13 at 21:07
-
@Yar it's 10x faster if you will compare char* with C function strcmp – Volodymyr B. Sep 07 '13 at 13:22
-
1@SAKrisT and that logic can be applied to almost all uses of Objective-C vs. C. If you've got the training/expertise, go for the 10x faster. For the rest of us, code that can be trusted and easily understood is worth the trade-off. Objective-C is still quite fast, especially for those of us coming from VM langs like Java and Interpreted langs like Ruby. – Dan Rosenstark Sep 14 '13 at 00:22
-
Also you can use NAMEOF(obj) instead of class_getName(obj). From objc.h header - #define NAMEOF(obj) object_getClassName(obj) – HotJard Oct 17 '13 at 09:37
-
1Your example of `[a class]` is confusing to a beginner. It would be better to use something like `[UIButton class]`. – The Muffin Man Jan 06 '14 at 00:57
-
Or simpler: `const char* className = object_getClassName(yourObject);` – Slipp D. Thompson Feb 23 '15 at 02:09
-
I should note that calling `[yourObject class]` leaves `yourObject` free to override the `class` implementation and return anything it wants. While unusual, this isn't unheard of in situations like `NSProxy`s, `NSDistantObject`s, and the like. `object_getClassName()` asks the runtime directly what the real class of instance is— it's similar to reading the `yourObject->isa` struct field in the old Obj-C 1 runtime. – Slipp D. Thompson Feb 23 '15 at 20:28
You also can use
NSString *className = [[myObject class] description];
on any NSObject

- 709
- 6
- 14
-
8This may or may not work based on whether the programmer has overridden the description method. Using [object class] or NSStringFromClass always returns the class name, though. – futureelite7 Jan 08 '13 at 01:01
-
1
What means about isKindOfClass in Apple Documentation
Be careful when using this method on objects represented by a class cluster. Because of the nature of class clusters, the object you get back may not always be the type you expected. If you call a method that returns a class cluster, the exact type returned by the method is the best indicator of what you can do with that object. For example, if a method returns a pointer to an NSArray object, you should not use this method to see if the array is mutable, as shown in the following code:
// DO NOT DO THIS!
if ([myArray isKindOfClass:[NSMutableArray class]])
{
// Modify the object
}
If you use such constructs in your code, you might think it is alright to modify an object that in reality should not be modified. Doing so might then create problems for other code that expected the object to remain unchanged.

- 47
- 4
-
Your example code is not correct. You will only run into problems if you test for NSArray, which is immutable anyway. If you want to test for a specific class, you should use isMemberOfClass instead. – futureelite7 Jan 23 '14 at 04:02
-
@futureelite7 This is an example of how *not* to test for class type. The [Apple documentation](https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Protocols/NSObject_Protocol/#//apple_ref/occ/intfm/NSObject/isKindOfClass:) states that `-isKindOfClass:` & `-isMemberOfClass:` should not be used to identify objects within a class cluster. – pxpgraphics Aug 18 '15 at 17:05
If you want to check for a specific class then you can use
if([MyClass class] == [myClassObj class]) {
//your object is instance of MyClass
}

- 39,458
- 17
- 135
- 184
if you want to get the name of the class simply call:-
id yourObject= [AnotherClass returningObject];
NSString *className=[yourObject className];
NSLog(@"Class name is : %@",className);

- 3,490
- 2
- 19
- 49
You can also check run time. Put one breakpoint in code and inside (lldb) console write
(lldb) po [yourObject class]
Like this..

- 11,242
- 5
- 69
- 122