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的任何子类声明的所有属性,或者它的子类.我面临的问题是,现在,这段代码返回了我的子类中未定义的无关属性.propertiesNames带有@“superclass”,@“description”,@“debugDescription”,@“hash”对于某些类,即使我从未在子类中的任何位置定义过这些属性.
奇怪的是,对于CBLnestedModel的所有子类,不返回这些无关的属性,但仅针对某些子类.但是,在我的应用程序的每次运行中,它们都将可靠地返回给那些子类.
知道为什么会这样吗?
解决方法
@interface FOOObject : NSObject<NScopying,FOOOtherProtocol>
看起来很好,它是NSObject的直接子类.事实上,我有其他对象来测试它,一个具有属性,一个没有.
@interface FOOObjectWithProperties : NSObject @property Nsstring *someProperty; @end
和
@interface FOOObjectWithoutProperties : NSObject @end
在测试期间,FOOObjectWithProperties和FOOObjectWithoutProperties都没有包含前面提到的四个NSObject属性,而是原始的FOOObject DID.
那有什么区别?好好看看NScopying它似乎没有添加任何属性所以我查看了FOOOtherProtocol,我已经实现了一些协议,它也没有声明任何属性.
然而
看看FOOOtherProtocol的声明:
@protocol FOOOtherProtocol<NSObject>
它就是. objective-c运行时东西不包括返回的超类属性,但它包括在协议扩展中声明的属性(强制遵守其他协议的协议).
注意有关hash,superclass和debugDescription的任何内容?
看看它们在NSObject protocol declaration中的声明位置
从子类的协议中删除强制的NSObject协议遵从性(因为它们是NSObject的子类,无论如何已经遵守它),你应该看到这些属性消失了.