使用Objective-C运行时检索Objective-C类所有实例变量及其对应类型的代码
它也支持超级类
要运行示例项目,请克隆仓库,并首先从Example目录运行pod install
。
Ravi Prakash Sahu,[email protected]
ClassProperty在MIT许可证下提供。有关更多信息,请参阅LICENSE文件。
假设我们有三个类
RSClassA
@interface RSClassA : NSObject
@property NSString *alpha;
@property NSNumber *beta;
@property NSData *gamma;
@property NSArray *listArray;
@end
RSClassB
@interface RSClassB : RSClassA
@property NSString *intel;
@property NSString *mac;
@end
RSClassC
@interface RSClassC : RSClassB
@property NSInteger count;
@property CGFloat height;
@property NSIndexPath *indexPath;
@end
只需导入此
#import <ClassProperty/ClassProperty.h>
示例代码
NSDictionary *propertyDict = [ClassProperty getPropertyDictionaryForClass:[RSClassC class]];
for (NSString *key in [propertyDict allKeys]) {
NSLog(@"Key : %@, Type : %@", key, [propertyDict valueForKey:key]);
}
或者
NSDictionary *propertyDict = [ClassProperty getPropertyDictionaryForClass:NSClassFromString(@"RSClassC")];
for (NSString *key in [propertyDict allKeys]) {
NSLog(@"Key : %@, Type : %@", key, [propertyDict valueForKey:key]);
}