Just made a tiny method for this.
// Simple as. Class propertyClass = [customObject classOfPropertyNamed:propertyName];
It can be optimized in many ways, but I like it.
The implementation is as follows:
-(Class)classOfPropertyNamed:(NSString*) propertyName { // Get Class of property to be populated. Class propertyClass = nil; objc_property_t property = class_getProperty([self class], [propertyName UTF8String]); NSString *propertyAttributes = [NSString stringWithCString:property_getAttributes(property) encoding:NSUTF8StringEncoding]; NSArray *splitPropertyAttributes = [propertyAttributes componentsSeparatedByString:@","]; if (splitPropertyAttributes.count > 0) { // xcdoc://ios//library/prerelease/ios/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtPropertyIntrospection.html NSString *encodeType = splitPropertyAttributes[0]; NSArray *splitEncodeType = [encodeType componentsSeparatedByString:@"\""]; NSString *className = splitEncodeType[1]; propertyClass = NSClassFromString(className); } return propertyClass; }
This is part of eppz! kit , in a developing object declaration called NSObject+EPPZRepresentable.h
. It actually does what you must achieve initially.
It encodes many types, iterates over collections, represents CoreGraphics, UIColors types, and also presents / restores object references.
The new version also returns you the names of C types and with the name struct types :
NSLog(@"%@", [self typeOfPropertyNamed:@"index"]); // unsigned int NSLog(@"%@", [self typeOfPropertyNamed:@"area"]); // CGRect NSLog(@"%@", [self typeOfPropertyNamed:@"keyColor"]); // UIColor
Part of the eppz model! feel free to use method implementations on <a3>
source share