獲取NSObject的所有屬性

NSObject+Property.h

#import <Foundation/Foundation.h>

@interface NSObject (Property)

- (NSDictionary*)properties;

@end

NSObject+Property.m

#import "NSObject+Property.h"
#import <objc/runtime.h>

@implementation NSObject (Property)

- (NSDictionary *)properties{
    unsigned int outCount,index;
    objc_property_t* properties_t = class_copyPropertyList([self class], &outCount);
    NSMutableDictionary *resultDictionary = [[NSMutableDictionary alloc] init];
    for(index = 0 ; index < outCount ; index ++){
        objc_property_t t_property = properties_t[index];
        NSString *attributeName = [NSString stringWithUTF8String:property_getName(t_property)];
        [resultDictionary setObject:[self valueForKey:attributeName]
                             forKey:attributeName];
    }
    NSDictionary *res = [NSDictionary dictionaryWithDictionary:resultDictionary];
    [resultDictionary release];
    return res;
}

@end





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章