152

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?

2
  • Perhaps reword your question or accept the answer that matches the question? Commented Nov 7, 2013 at 12:33
  • @JasperBlues: Done, didn't even realize how popular this got! Commented Nov 7, 2013 at 16:05

8 Answers 8

418

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

Sign up to request clarification or add additional context in comments.

2 Comments

Upvoted for answering the question so that googlers can get an answer, instead of anticipating the need.
Remember to #import <objc/objc-runtime.h> to able to call class on an instance.
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]];

Comments

17

From within the class itself

-(NSString *) className
{
    return NSStringFromClass([self class]);
}

5 Comments

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 I see you are hunting me down. Good for you!
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).
This adds nothing to the answer except trouble.
Should make this a class method
6

OBJC:

NSStringFromClass([instance class])

SWIFT

From instance:

String(describing: YourType.self)

From type:

String(describing: self)

Comments

2

You can also use [[self class] description]

Comments

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.

1 Comment

If called on a Swift class, this returns a namespaced classname.
1

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

1 Comment

or just self.dynamicType
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")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.