如何找到符合 KVC 标准的 Objective-C 对象的所有属性键? [英] How do I find all the property keys of a KVC compliant Objective-C object?

查看:25
本文介绍了如何找到符合 KVC 标准的 Objective-C 对象的所有属性键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种方法可以返回符合 NSKeyValueCoding 协议的对象的所有键?

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

[object getPropertyKeys] 类似的东西将返回 NSString 对象的 NSArray.它适用于任何 KVC 兼容的对象.有这样的方法吗?到目前为止,我在搜索 Apple 文档时没有发现任何内容.

Something along the lines of [object getPropertyKeys] that would return an NSArray of NSString objects. It would work for any KVC-compliant object. Does such a method exist? I haven't found anything in searching the Apple docs so far.

谢谢,G.

推荐答案

#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);

这篇关于如何找到符合 KVC 标准的 Objective-C 对象的所有属性键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆