我想要获取我的类或其任何子类定义的所有属性列表。以下代码片段是我一直在使用的代码,一直到最近的iOS8 beta 4都可以正常工作。
if(!dictionary) {
dictionary = [NSMutableDictionary dictionary];
// Get all properties we have until we hit CBLNestedModel
while(klass != [CBLNestedModel class]) {
unsigned count;
objc_property_t* properties = class_copyPropertyList(klass, &count);
for (unsigned i = 0; i < count; i++) {
objc_property_t property = properties[i];
const char* propertyNameC = property_getName(property);
NSString* propertyName = [NSString stringWithUTF8String:propertyNameC];
const char* propertyAttrC = property_getAttributes(property);
NSString* propertyAttrS = [NSString stringWithUTF8String:propertyAttrC];
NSArray* propertyAttr = [propertyAttrS componentsSeparatedByString:@","];
NSLog(@"%@ has property %@", NSStringFromClass(klass), propertyName);
dictionary[propertyName] = propertyAttr;
}
free(properties);
klass = [klass superclass];
}
propertyDictionary[klassString] = dictionary;
}
CBLNestedModel派生自NSObject。基本上,我想要任何CBLNestedModel子类或其子类声明的所有属性。我面临的问题是,现在,这段代码返回了不属于我的子类中定义的多余属性。对于某些类,propertyNames会返回 @"superclass",@"description",@"debugDescription",@"hash"
,即使我从未在我的子类中定义过这些属性。
奇怪的是,这些多余属性并不会为CBLNestedModel的所有子类返回,而只会为某些子类返回。但是,它们将可靠地在每次运行我的应用程序时返回这些子类。
你有什么想法为什么现在会发生这种情况吗?
superclass
、description
、debugDescription
和hash
不是属性,而是由NSObject
实现的NSObjectProtocol
方法。第一个问题:这是一个错误吗?第二个问题:我们应该创建一个雷达吗? - Martinhash
、description
、superclass
和debugDescription
。你能否分享更多关于你的子类的信息,以帮助发现问题?或者有关报告的更新? - Dean Kelly