LoginSignup
26

More than 5 years have passed since last update.

オブジェクトのプロパティ名の一覧とプロパティ値の一覧を取得

Last updated at Posted at 2013-06-25

オブジェクトに含まれるプロパティ名とプロパティ値の一覧を取得するためには、Objective-C のランタイムを利用します。

#import "objc/runtime.h"

をImportして以下のコードで一覧を取得可能です。

unsigned int outCount, i;
objc_property_t *properties = class_copyPropertyList([(任意のオブジェクト) class], &outCount);
for (i = 0; i < outCount; i++) {
   objc_property_t property = properties[i];
   const char *name = property_getName(property);
   NSString *propertyName = [NSString stringWithUTF8String:name];
   NSString *propertyValue = [(任意のオブジェクト) valueForKey:propertyName]; 
   NSLog(@"プロパティ名[%d]:%@",i ,propertyName); 
   NSLog(@"プロパティ値[%d]:%@",i ,propertyValue);
 }
free(properties);

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
26