美文网首页
iOS 运行时获取指定属性的类型

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

作者: huangxiongbiao | 来源:发表于2017-05-11 17:08 被阅读919次

.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 运行时获取指定属性的类型

      本文链接:https://www.haomeiwen.com/subject/ymaotxtx.html