Is it possible to get the name of a class into a string like this:
NSString *kls = (NSString *)[self.name class];
NSLog(@"%@", [kls isKindOfClass:[NSString class]] ? @"YES" : @"NO");
I can do: NSString *kls = [[[NSString alloc]initWithFormat:@"%@", [self.name class]]autorelease];
but that seems a bit long-winded to me. It's not for an开发者_运维知识库y task in particular, I'm just trying to learn more about the language as I go.
NSString* classNameStr = NSStringFromClass( [anObject class] );
On the Mac, provided your object inherits from the NSObject
class rather than just implementing the NSObject
protocol (the latter situation seems sort of unlikely), you should be able to call:
NSString* str = [anObject className];
className
is defined in connection with Mac scripting support, so it is not available on the iOS version of NSObject
. In that case, you can probably do something like this:
NSString* str = [[anObject class] description];
(I have only actually tried this on a Mac, though, so it's possible it may not work correctly on an iOS device.)
精彩评论