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.
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.
There are two ways to access the instance variable of class:Within the class by using self and object reference. Using getattr() method.
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With