如何查找符合KVC的Objective-C对象的所有属性键? [英] How do I find all the property keys of a KVC compliant Objective-C object?
本文介绍了如何查找符合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。
Thanks, 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屋!
查看全文