tipo de propiedad o clase utilizando la reflexión
Me preguntaba si es posible determinar la clase o el tipo primitivo de las propiedades de los objetos. Obtener todos los nombres y valores de propiedades es bastante fácil.Pues contesta
Entonces, ¿hay alguna forma de obtener el tipo de clase de propiedades mientras que la propiedad no tiene valor o valor nulo?
Código de ejemplo
@interface MyObject : NSObject
@property (nonatomic, copy) NSString *aString;
@property (nonatomic, copy) NSDate *aDate;
@property NSInteger aPrimitive;
@end
@implementation MyObject
@synthesize aString;
@synthesize aDate;
@synthesize aPrimitive;
- (void)getTheTypesOfMyProperties {
unsigned int count;
objc_property_t* props = class_copyPropertyList([self class], &count);
for (int i = 0; i < count; i++) {
objc_property_t property = props[i];
// Here I can easy get the name or value
const char * name = property_getName(property);
// But is there any magic function that can tell me the type?
// the property can be nil at this time
Class cls = magicFunction(property);
}
free(props);
}
@end