Objective-C's @encode
produces C strings to represent any type, including primitives, and classes, like so:
NSLog(@"%s", @encode(int)); // i
NSLog(@"%s", @encode(float)); // f
NSLog(@"%s", @encode(CGRect)); // {CGRect={CGPoint=ff}{CGSize=ff}}
NSLog(@"%s", @encode(NSString)); // {NSString=#}
NSLog(@"%s", @encode(UIView)); // {UIView=#@@@@fi@@I{?=b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b6b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b3b1b1b1b2b2b1}}
So, I can get a meaningful encoding of a class (one that contains the class name) using @encode(ClassName)
, but it's also in the same format as the encoding of a generic struct
(as in the above example).
Now, my question is, given any (valid of course) type encoding, is it possible to find out whether the encoding is of an Objective-C class, and if so, to get the Class
object that corresponds to that encoding?
Of course, I probably could just try to parse the class name out of the type encoding, and get the class from that using NSClassFromString
, but that just doesn't sound like the right way to do it, or particularly performance-efficient. Is this really the best way to achieve this?