Serhats の解決策は素晴らしいですが、残念ながら iOS では機能しません (前述のとおり) (この質問は iOS 用にタグ付けされています)。回避策は、オブジェクトの NSDictionary 表現を取得し、キーと値のペアとして通常どおりアクセスすることです。NSObject のカテゴリをお勧めします。
ヘッダー ファイル:
@interface NSObject (NSDictionaryRepresentation)
/**
Returns an NSDictionary containing the properties of an object that are not nil.
*/
- (NSDictionary *)dictionaryRepresentation;
@end
実装ファイル:
#import "NSObject+NSDictionaryRepresentation.h"
#import <objc/runtime.h>
@implementation NSObject (NSDictionaryRepresentation)
- (NSDictionary *)dictionaryRepresentation {
unsigned int count = 0;
// Get a list of all properties in the class.
objc_property_t *properties = class_copyPropertyList([self class], &count);
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] initWithCapacity:count];
for (int i = 0; i < count; i++) {
NSString *key = [NSString stringWithUTF8String:property_getName(properties[i])];
NSString *value = [self valueForKey:key];
// Only add to the NSDictionary if it's not nil.
if (value)
[dictionary setObject:value forKey:key];
}
free(properties);
return dictionary;
}
@end
この記事から借用: http://hesh.am/2013/01/transform-properties-of-an-nsobject-into-an-nsdictionary/
このようにして、serhatsが言及したのと同様のことができます:
for (NSString *key in objectDic.allKeys) {
if([objectDic[key] isKindOfClass:[UILabel class]])
{
//put attribute to your array
}
}