+(NSMutableDictionary *)generateDict:(id)model{
NSMutableDictionary *nameOrTypeDict = [self allPropertyNames:[model class]];
NSMutableArray *allNames = nameOrTypeDict[@"name"];
NSMutableArray *allType = nameOrTypeDict[@"type"];
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
for (int i = 0; i < allNames.count; ++i) {
id str = [model valueForKey:allNames[i]];
NSString *type = allType[i];
// NSLog(@"打印%@--%@",str,type);
if (str != nil) {
if ([type isEqualToString:@"Bool"]){
NSLog(@"判断%@",str);
str = [NSString stringWithFormat:@"%@",str];
if (![str isEqualToString:@"0"]) {
[dict setValue:@"1" forKey:allNames[i]];
}else{
[dict setValue:@"0" forKey:allNames[i]];
}
}else if([type isEqualToString:NSStringFromClass([NSString class])]){
str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[dict setValue:str forKey:allNames[i]];
}else if([str isKindOfClass:[NSDictionary class]]){
NSDictionary *dict1 = (NSDictionary *)str;
NSMutableDictionary *muDict = [NSMutableDictionary dictionary];
[dict1 enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
if ([obj isKindOfClass:[NSString class]]) {
obj = [obj stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
}
[muDict setValue:obj forKey:key];
}];
[dict setValue:muDict forKey:allNames[i]];
}else if([str isKindOfClass:[NSArray class]]){
NSArray *arry = (NSArray *)str;
NSMutableArray *array = [NSMutableArray array];
[arry enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@"%@", obj);
if([obj isKindOfClass:[XSMain class]]){
obj = [self generateDict:obj];
}else if ([obj isKindOfClass:[NSString class]]) {
obj = [obj stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
}
[array addObject:obj];
}];
[dict setValue:array forKey:allNames[i]];
}else if([str isKindOfClass:[XSMain class]]){
NSDictionary *dict1 = [self generateDict:str];
[dict setValue:dict1 forKey:allNames[i]];
}else{
[dict setValue:str forKey:allNames[i]];
}
}
}
return dict;
}
///通过运行时获取当前对象的所有属性的名称,以数组的形式返回
+ (NSMutableDictionary *) allPropertyNames:(Class)class{
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
///存储所有的属性名称
NSMutableArray *allNames = [[NSMutableArray alloc] init];
NSMutableArray *allType = [[NSMutableArray alloc] init];
///存储属性的个数
unsigned int propertyCount = 0;
Ivar * ivarList = class_copyIvarList(class, &propertyCount);
///通过运行时获取当前类的属性
objc_property_t *propertys = class_copyPropertyList(class, &propertyCount);
//把属性放到数组中
for (int i = 0; i < propertyCount; i ++) {
///取出第一个属性
// objc_property_t property = propertys[i];
Ivar ivar = ivarList[i];
// const char * propertyName = property_getName(property);
NSString *name = [NSString stringWithUTF8String:ivar_getName(ivar)];
if ([name containsString:@"_"]) {
name = [name substringFromIndex:1];
}
NSString *type = [NSString stringWithUTF8String:ivar_getTypeEncoding(ivar)];
if ([type isEqualToString:@"i"]) {
type = @"int";
}
if ([type isEqualToString:@"B"]) {
type = @"Bool";
}
if ([type isEqualToString:@"f"]) {
type = @"float";
}
if ([type isEqualToString:@"d"]) {
type = @"double";
}
if ([type containsString:@"\""]) {
NSRange range = [type rangeOfString:@"\""];
type = [type substringFromIndex:range.location + range.length];
range = [type rangeOfString:@"\""];
type = [type substringToIndex:range.location];
}
[allType addObject:type];
[allNames addObject:name];
}
// NSArray* reversedArray = [[allType reverseObjectEnumerator] allObjects];
dict[@"type"] = allType;
dict[@"name"] = allNames;
///释放
free(propertys);
return dict;
}