Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting name of the class from an instance

People also ask

How do I find the class name of an object?

If you have a JavaSW object, you can obtain it's class object by calling getClass() on the object. To determine a String representation of the name of the class, you can call getName() on the class.

What is class name in Python?

A class in Python can be defined using the class keyword. As per the syntax above, a class is defined using the class keyword followed by the class name and : operator after the class name, which allows you to continue in the next indented line to define class members. The followings are class members.

How do I get an instance of a class in Python?

There are two ways to access the instance variable of class:Within the class by using self and object reference. Using getattr() method.

How do you return a class name in Java?

The java. lang. Class. getName() returns the name of the entity (class, interface, array class, primitive type, or void) represented by this Class object, as a String.


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


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

From within the class itself

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

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.


OBJC:

NSStringFromClass([instance class])

SWIFT

From instance:

String(describing: YourType.self)

From type:

String(describing: self)

You can also use [[self class] description]