The default description for a class instance returns "ClassName: 0x105120". How might I modify the method below to just return the "ClassName"?
// The code below just returns the address ...
- (NSString *)description {
NSString *result;
result = [NSString stringWithFormat:@"%p", self];
return result;
}
EDIT: in that case would this be correct? Although I do understand that if I want to actually get the className as an NSString I should use NSStringFromClass([self class])
- (id)init {
NSLog(@"_init: %@", [self class]);
[super init];
return self;
}
thanks in advance -gary-
It returns the Class object that represents the specified class name. This is used if you need to get the Class object. This roughly corresponds to . getClass() which returns the Class object that corresponds to the object instance.
Returning a Class or InterfaceA method can have the class name as its return type. Therefore it must return the object of the exact class or its subclass. An interface name can also be used as a return type but the returned object must implement methods of that interface.
The simplest way is to call the getClass() method that returns the class's name or interface represented by an object that is not an array. We can also use getSimpleName() or getCanonicalName() , which returns the simple name (as in source code) and canonical name of the underlying class, respectively.
iPhoneOS: NSStringFromClass([self class])
MacOS: [self className]
... gives you an NSString with the class name
Edit:
For both iPhoneOS and MacOS the way to go is:
NSStringFromClass([self class])
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