@interface JCF_FMDBRunTimeModelInterface : NSObject{
Class _modelClass;
}
- (instancetype)initWithModelClassName:(NSString *)className;
/*
* 主鍵的屬性名
*/
@property (nonatomic,strong)NSString *mainKeyPropertyName;
@property (nonatomic,strong)NSArray *modelPropertyNameArray;
@property (nonatomic,assign)BOOL haveModelArrayProperty;
@property (nonatomic,assign)BOOL haveModelProperty;
/*
* NSDictionary <NSString *,NSString *> 中
* propertyModelClassName model的class string
* propertyName 對應(yīng)的屬性名字
*/
@property (nonatomic,strong)NSArray <NSDictionary <NSString *,NSString *>*>* modelArrayPropertyArray;
@property (nonatomic,strong)NSArray <NSDictionary <NSString *,NSString *>*>* modelPropertyArray;
@end
@implementation JCF_FMDBRunTimeModelInterface
- (instancetype)initWithModelClassName:(NSString *)className{
if (self = [super init]) {
_modelClass = NSClassFromString(className);
}
return self;
}
- (NSString *)mainKeyPropertyName{
if ([_modelClass respondsToSelector:@selector(mainKey)]) {//相應(yīng)類方法
return [_modelClass mainKey];
}
NSString *mainKey = [[[_modelClass alloc] init] mainKey];
if (mainKey.length == 0) {
JCF_Exception(NSStringFromClass(_modelClass),@"必須至少實現(xiàn)一個獲取主鍵的方法")
}
return mainKey;
}
- (BOOL)haveModelProperty{
return self.modelPropertyArray.count != 0;
}
- (BOOL)haveModelArrayProperty{
return self.modelArrayPropertyArray.count != 0;
}
- (Class)propertyClassBy:(SEL)selector{
// 下面這段代碼抄自Mantle 方法實現(xiàn)的調(diào)用~
IMP imp = [_modelClass methodForSelector:selector];
Class (*function)(id, SEL) = (__typeof__(function))imp;
Class class = function(_modelClass, selector);
return class;
}
- (NSArray<NSDictionary<NSString *,NSString *> *> *)modelPropertyArray{
if (!_modelPropertyArray) {
NSMutableArray *modelPropertyArray = [NSMutableArray array];
for (NSString *propertyNameString in self.modelPropertyNameArray) {
NSString *selectorString = [NSString stringWithFormat:@"%@JCFModel",propertyNameString];
SEL selector = NSSelectorFromString(selectorString);
if ([_modelClass respondsToSelector:selector]) {
Class class = [self propertyClassBy:selector];
NSString *className = NSStringFromClass(class);
[modelPropertyArray addObject:@{propertyNameString:className}];
}
}
_modelPropertyArray = modelPropertyArray.copy;
}
return _modelPropertyArray;
}
- (NSArray<NSDictionary<NSString *,NSString *> *> *)modelArrayPropertyArray{
if (!_modelArrayPropertyArray) {
NSMutableArray *modelArrayPropertyArray = [NSMutableArray array];
for (NSString *propertyNameString in self.modelPropertyNameArray) {
NSString *selectorString = [NSString stringWithFormat:@"%@JCFModelArray",propertyNameString];
SEL selector = NSSelectorFromString(selectorString);
if ([_modelClass respondsToSelector:selector]) {
Class class = [self propertyClassBy:selector];
NSString *className = NSStringFromClass(class);
[modelArrayPropertyArray addObject:@{propertyNameString:className}];
}
}
_modelArrayPropertyArray = modelArrayPropertyArray.copy;
}
return _modelArrayPropertyArray;
}
- (NSArray *)modelPropertyNameArray{
if (!_modelPropertyNameArray) {
NSMutableArray *modelPropertyNameArray = [NSMutableArray array];
unsigned int count;
objc_property_t *propertys = class_copyPropertyList(_modelClass, &count);
for (int i = 0; i<count; i++) {
objc_property_t pro = propertys[i];
const char *cName = property_getName(pro);
NSString *proprttyNameString = [NSString stringWithCString:cName encoding:NSUTF8StringEncoding];
[modelPropertyNameArray addObject:proprttyNameString];
}
_modelPropertyNameArray = modelPropertyNameArray.copy;
}
return _modelPropertyNameArray;
}
@end
//model
#import "ModelBase.h"
#import "CeShiCar.h"
#import "CeShiDog.h"
@interface CeShiPeople : ModelBase
@property (nonatomic, strong)NSString *name;
@property (nonatomic,strong)CeShiCar *car;
@property (nonatomic, strong)NSArray *dogs;
@end
@implementation CeShiPeople
+ (Class)carJCFModel{
return [CeShiCar class];
}
+ (Class)dogsJCFModelArray{
return [CeShiDog class];
}
@end
//USING
- (void)fmdb{
JCF_FMDBRunTimeModelInterface *xx = [[JCF_FMDBRunTimeModelInterface alloc] initWithModelClassName:NSStringFromClass([CeShiPeople class])];
NSArray *arr0 = xx.modelPropertyNameArray;
NSArray *arr1 = xx.modelPropertyArray;
NSArray *arr2 = xx.modelArrayPropertyArray;
arr0;
}
//RESULT
Printing description of arr0:
<__NSArrayI 0x17005d880>(
name,
car,
dogs
)
Printing description of arr1:
<__NSSingleObjectArrayI 0x170008310>(
{
car = CeShiCar;
}
)
Printing description of arr2:
<__NSSingleObjectArrayI 0x170008320>(
{
dogs = CeShiDog;
}
)