properties - How to determine class of Objective-C Property? -
ok, have property on class:
@interface myobject : nsobject { myotherobject *someotherobject; } @property (nonatomic, copy) myotherobject *someotherobject; @end
i'm trying create deserialize api response correct objects without explicitly defining class each api attribute maps to. so, there way me find out class property is, if it's nil
? i'm looking method [myobject classforproperty:@"someotherobject"]
return class
object. note--i'm not looking iskindofclass:
or class
, won't work on nil
value.
worst case, can manually define list of property classes, it'd nice if there built in way it.
ok, ended figuring out after coming across question: in objective-c determine if property int, float, double, nsstring, nsdate, nsnumber, etc
here's code:
@interface nsobject (properties) + (class)classforproperty:(nsstring *)propertyname; @end #import <objc/runtime.h> @implementation nsobject (properties) + (class)classforproperty:(nsstring *)propertyname { const char *attributes = property_getattributes(class_getproperty([self class], [propertyname utf8string])); nsstring *attributesstring = [nsstring stringwithutf8string:attributes]; nsarray *parts = [attributesstring componentsseparatedbystring:@","]; nsstring *typestring = [[parts objectatindex:0] substringfromindex:1]; if ([typestring hasprefix:@"@"] && [typestring length] > 1) { nsstring *name = [typestring substringwithrange:nsmakerange(2, [typestring length]-3)]; class theclass = nsclassfromstring(name); return theclass; } return nil; } @end
it seems work pretty me, modifications welcome.
Comments
Post a Comment