iOS 运行时获取指定属性的类型

.h

//获取属性的类型
+(NSString*)getPropertyType:(NSString*)property;

.m

#pragma mark-------//获取属性的类型

+(NSString *)getPropertyType:(NSString *)property {
    //获取对象的类型objc_getClass("UserModel")
    objc_property_t p = class_getProperty(self, property.UTF8String);
    const char *cname = property_getAttributes(p);
    DDLog(@"%s==",cname);
    // 2.成员类型
    NSString *attrs = @(property_getAttributes(p));
    NSUInteger dotLoc = [attrs rangeOfString:@","].location;
    NSString *code = nil;
    NSUInteger loc = 3;
    if (dotLoc == NSNotFound) { // 没有,
        code = [attrs substringFromIndex:loc];
    } else {
        code = [attrs substringWithRange:NSMakeRange(loc, dotLoc - loc-1)];
    }
    DDLog(@"%@===%@====",code,attrs);
    return code;
}

你可能感兴趣的:(iOS 运行时获取指定属性的类型)