zoukankan      html  css  js  c++  java
  • 利用runtime检測这个对象是否存在某属性?

    假定有实例对象—instance,我们怎样知道该实例对象是否存在属性—propertyName?

    利用runtime,我们能够获取到它的属性列表

    1)属性列表

        unsigned int outCount, i;

        // 获取对象里的属性列表

        objc_property_t * properties = class_copyPropertyList([instance class], &outCount);


    2)验证propertyName是否在属性列表中

    通过遍历属性列表,如:


    BOOL  isExist = NO;

    for (i = 0; i < outCount; i++) {

            objc_property_t  property =properties[i];

            // 属性名转成字符串

            NSString *tempPropertyName = [[NSString alloc] initWithCString:property_getName(property) encoding:NSUTF8StringEncoding];

            // 推断该属性是否存在

            if ([tempPropertyName isEqualToString:propertyName]) {

                free(properties);

                isExist = YES;

            }

    }






  • 相关阅读:
    招聘.NET开发人员
    SQL 2005 SSIS 导入数据效率问题
    用户控件使用事件与调用页面交互
    使用sql语句删除标识列属性
    poj1520
    poj1476
    poj1363
    poj1477
    poj1312
    大端法小端法与union
  • 原文地址:https://www.cnblogs.com/wzzkaifa/p/7130920.html
Copyright © 2011-2022 走看看