iOSアプリ開発の逆引き辞典

iPhone/iPadで使えるアプリ開発のTipsをまとめてみました

クラス名からそのクラスの持っているプロパティの一覧を取得する

クラス名からそのクラスの持っているプロパティの一覧を取得する方法をご紹介します。ログ出力に便利になると思います。

#import <objc/runtime.h>

// クラス名からプロパティリストを生成する
id lenderClass = objc_getClass([className UTF8String]);
unsigned int propertyCount = 0;
objc_property_t* propertyList = class_copyPropertyList(lenderClass, &propertyCount);

NSMutableArray* propList = [[[NSMutableArray alloc] init] autorelease];
for (int i = 0; i < propertyCount; i++) {
    objc_property_t* property = propertyList + i;
    NSString* propertyName = [NSString stringWithCString:property_getName(*property) encoding:NSASCIIStringEncoding];
    NSLog(@"propertyName: %@", propertyName);
}
free(propertyList);