未知のクラスのすべてのプロパティとすべてのプロパティのクラスのリストを取得しようとしています。その瞬間、私はオブジェクトのすべてのプロパティのリストを取得します(私はすべてのスーパークラスを取得するために再帰的に行います)。iOS取得プロパティクラス
NSArray* properties = [PropertyUtil classPropsFor:[self class]];
for (NSString* property in properties) {
id value= [self valueForKey:property];
NSLog(@"Value class for key: %@ is %@", property, [value class]);
}
問題は、それがNSStringsためかではなく、カスタムクラスのため、それは私を返すことのために働くです:私は、だから今、私はすべてのプロパティのクラスをしたいと私はthis post
+ (NSArray *)classPropsFor:(Class)klass
{
NSLog(@"Properties for class:%@", klass);
if (klass == NULL || klass == [NSObject class]) {
return nil;
}
NSMutableArray *results = [[NSMutableArray alloc] init];
unsigned int outCount, i;
objc_property_t *properties = class_copyPropertyList(klass, &outCount);
for (i = 0; i < outCount; i++) {
objc_property_t property = properties[i];
const char *propName = property_getName(property);
if(propName) {
NSString *propertyName = [NSString stringWithUTF8String:propName];
[results addObject:propertyName];
}
NSArray* dict = [self classPropsFor:[klass superclass]];
[results addObjectsFromArray:dict];
}
free(properties);
return [NSArray arrayWithArray:results];
}
に触発しましたヌル。私は再帰的に内部に他のオブジェクトを持つことができるオブジェクトを表す辞書を作成したいと思います。私はすべてのプロパティのクラスを知る必要があると思っています。
素晴らしい人で完璧です!あなたの助けをありがとう:) – Jpellat
あなたは大歓迎です:) –