How do I find all the property keys of a KVC compliant Objective-C object?

后端 未结 5 576
灰色年华
灰色年华 2020-12-13 01:03

Is there a method that returns all the keys for an object conforming to the NSKeyValueCoding protocol?

Something along the lines of [object getPropertyKeys]

5条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-13 01:23

    #import "objc/runtime.h"
    
    unsigned int outCount, i;
    
    objc_property_t *properties = class_copyPropertyList([self class], &outCount);
    for(i = 0; i < outCount; i++) {
        objc_property_t property = properties[i];
        const char *propName = property_getName(property);
        if(propName) {
                const char *propType = getPropertyType(property);
                NSString *propertyName = [NSString stringWithUTF8String:propName];
                NSString *propertyType = [NSString stringWithUTF8String:propType];
        }
    }
    free(properties);
    

提交回复
热议问题