+(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])]){

该博客介绍了如何使用Objective-C的运行时(runtime)将模型转换为字典。通过遍历模型的所有属性,根据属性类型进行相应的值转换,如Bool转字符串,字符串URL编码,处理嵌套的字典和数组等。并提供了`generateDict:`方法实现这一过程。
最低0.47元/天 解锁文章
9292

被折叠的 条评论
为什么被折叠?



