#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
#import "ViewController.h"
#import "MyTestObj.h"
#import <objc/runtime.h>
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
MyTestObj* M = [[MyTestObj alloc] init];
unsigned PropertiesCount = 0;
objc_property_t* properties = class_copyPropertyList([M class], &PropertiesCount);
for (int i = 0; i < PropertiesCount; i++) {
objc_property_t prop = properties[i];
NSLog(@"%s",property_getName(prop));
NSLog(@"prop-attribute is %s",property_getAttributes(prop));
unsigned PropAttributesCount = 0;
objc_property_attribute_t* propAttributes = property_copyAttributeList(prop, &PropAttributesCount);
for (NSInteger j = 0; j < PropAttributesCount; j++) {
objc_property_attribute_t Attribute = propAttributes[0];
NSLog(@"name is %s, vlaue is %s",Attribute.name,Attribute.value);
}
}
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
1,实现了获取运行是对象的属性信息。
2,只针对对象本身的类型,不找父类的属性。
参考:http://stephen830.iteye.com/blog/1455523