151

I have the following problem: I get an instance of a class passed and want to know the name of the class of this instance. How to get this?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Robin
  • 8,197
  • 11
  • 45
  • 74

8 Answers8

413

NSStringFromClass([instance class]) should do the trick.

CiNN
  • 9,752
  • 6
  • 44
  • 57
31

if all you want to do is test an object to see if it's a type of a certain Class

BOOL test = [self isKindOfClass:[SomeClass class]];
kubi
  • 48,104
  • 19
  • 94
  • 118
17

From within the class itself

-(NSString *) className
{
    return NSStringFromClass([self class]);
}
Katedral Pillon
  • 14,534
  • 25
  • 99
  • 199
  • 2
    Absolutely don't want to arouse your anger, but this was the second iOS-related answer in your profile. It seems to me to add only that we can refer to an instance as "self" from within its implementation. Would you defend this as adding substantially to the three-year-old accepted answer? – danh Sep 17 '14 at 21:49
  • @danh I see you are hunting me down. Good for you! – Katedral Pillon Sep 17 '14 at 22:03
  • Sorry, I just clicked this one, and debated about whether to say anything. Just wanted to do some gentle ribbing, but I know that tempers get hot pretty quickly in these semi-faceless settings. Thanks for being good natured about it. (In fact, +1 for practicing encapsulation). – danh Sep 17 '14 at 22:07
  • 2
    This adds nothing to the answer except trouble. – Nikolai Ruhe Feb 08 '15 at 10:59
  • 1
    Should make this a class method – Bhumit Muchhadia Nov 14 '17 at 04:35
5

OBJC:

NSStringFromClass([instance class])

SWIFT

From instance:

String(describing: YourType.self)

From type:

String(describing: self)
Lal Krishna
  • 15,485
  • 6
  • 64
  • 84
2

You can also use [[self class] description]

Jeremie D
  • 4,145
  • 1
  • 35
  • 41
2

Just add a category:

NSObject+Extensions.h
- (NSString *)className;

NSObject+Extensions.m
- (NSString *)className {
    return NSStringFromClass(self.class);
}

Then use the following code:

NSString *className = [[SomeObject new] className];

or even:

NSString *className = SomeObject.new.className;

To use it anywhere add the category to YourProject.pch file.

Alper
  • 3,424
  • 4
  • 39
  • 45
ealee
  • 91
  • 6
0

If you are looking how get classname in Swift you can use reflect to get information about object.

let tokens = split(reflect(self).summary, { $0 == "." })
if let typeName = tokens.last {
    println(typeName)
}
pkamb
  • 33,281
  • 23
  • 160
  • 191
Roman Barzyczak
  • 3,785
  • 1
  • 30
  • 44
0
  • for instance
    • NSString* classNameNSStr = [someObjcInstance className]
  • for class
    • NSString* classNameNSStr = NSStringFromClass(someObjcClass)
    • const char* className = object_getClassName(someObjcClass)

--> related:

  • just want compare is some class or not
    • -> not need get class name, just need use isKindOfClass:
      • BOOL isSameClass = [someObjcInstance isKindOfClass: SomeClass];
        • SomeClass can get from: objc_getClass("SomeClassName")
crifan
  • 12,947
  • 1
  • 71
  • 56