酢ろぐ!

カレーが嫌いなスマートフォンアプリプログラマのブログ。

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

本記事は「クラス名からそのクラスの持っているプロパティの一覧を取得する - iOSアプリ開発の逆引き辞典」に転記しました。


こんなことをする人は、まぁいないと思いますが。ログ出力に便利なので。。。

#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);