プロパティ定義だけでアーカイブできるライブラリ LKCodingObject公開
class_copyPropertyList で取得できるプロパティには親クラスのリストが含まれないことに実際のプロジェクトで使っていたら気がついた。この為、親クラスで定義されているプロパティがアーカイブ/アンアーカイブの対象にならない。
そこで再帰的に親クラスを取得してプロパティ名を取得するように手を入れた。
- (void)_propertyNamesForClass:(Class)cls propertyNames:(NSMutableArray*)propertyNames
{
Class superClass = class_getSuperclass(cls);
if (superClass != [NSObject class]) {
[self _propertyNamesForClass:superClass propertyNames:propertyNames];
}
unsigned int count, i;
objc_property_t *objc_properties = class_copyPropertyList(cls, &count);
for(i = 0; i < count; i++) {
objc_property_t objc_property = objc_properties[i];
NSString* name = [NSString stringWithUTF8String:property_getName(objc_property)];
[propertyNames addObject:name];
}
free(objc_properties);
}
サブクラスのテストケースも加えておいた。
なお誤ってタグを 1.1にしてしまった(1.0は欠番)。
0 件のコメント:
コメントを投稿