設計模型
- 模型屬性,通常需要跟字典中的key一一對應秩铆,一個一個的生成模型屬性,很慢
- 我們提供一個字典分類灯变,專門根據(jù)字典生成對應的屬性字符串殴玛。
#import "NSDictionary+PropertyCode.h"
// isKindOfClass:判斷是否是當前類或者它的子類
@implementation NSDictionary (PropertyCode)
- (void)createPropertyCode
{
NSMutableString *strM = [NSMutableString string];
/*
解析字典,生成對應屬性代碼
1.遍歷字典,取出所有key,每個key對應一個屬性代碼
*/
[self enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull value, BOOL * _Nonnull stop) {
NSLog(@"%@ %@",key,[value class]);
NSString *code = nil;
if ([value isKindOfClass:[NSString class]]) {
// NSString
code = [NSString stringWithFormat:@"@property (nonatomic ,strong) NSString *%@;",key];
} else if ([value isKindOfClass:NSClassFromString(@"__NSCFBoolean")]){
// Bool
code = [NSString stringWithFormat:@"@property (nonatomic ,assign) BOOL %@;",key];
}else if ([value isKindOfClass:[NSNumber class]]){
// NSInteger
code = [NSString stringWithFormat:@"@property (nonatomic ,assign) NSInteger %@;",key];
}else if ([value isKindOfClass:[NSArray class]]){
// NSArray
code = [NSString stringWithFormat:@"@property (nonatomic ,strong) NSArray *%@;",key];
}else if ([value isKindOfClass:[NSDictionary class]]){
// NSDictionary
code = [NSString stringWithFormat:@"@property (nonatomic ,strong) NSDictionary *%@;",key];
}
// 獲取所有key
[strM appendFormat:@"\n%@\n",code];
}];
NSLog(@"%@",strM);
}
@end
字典轉模型
方式一:KVC
- 弊端:必須保證,模型中的屬性和字典中的key一一對應
- 如果不一致添祸,就會調(diào)用setValue:forUndefinedKey:報key
找不到的錯滚粟。
- 解決:重寫對象的setValue:forUndefinedKey: ,把系統(tǒng)的方法覆蓋刃泌, 就能繼續(xù)使用KVC凡壤,字典轉模型了
- (void)setValue:(id)value forUndefinedKey:(NSString *)key
{
}
方式二:Runtime
- 思路:利用運行時署尤,遍歷模型中所有屬性,根據(jù)模型的屬性名亚侠,去字典中查找key曹体,取出對應的值,給模型的屬性賦值盖奈。
- 步驟:提供一個NSObject分類混坞,專門字典轉模型,以后所有模型都可以通過這個分類轉钢坦。
#import <Foundation/Foundation.h>
@protocol ModelDelegate <NSObject>
@optional
// 提供一個協(xié)議究孕,只要準備這個協(xié)議的類,都能把數(shù)組中的字典轉模型
爹凹、厨诸、用在三級數(shù)組轉換
+ (NSDictionary *)arrayContainModelClass;
@end
@interface NSObject (Item)
// 字典轉模型
+ (instancetype)objectWithDict:(NSDictionary *)dict;
@end
#import "NSObject+Item.h"
#import <objc/message.h>
/*
* 把字典中所有value給模型中屬性賦值,
* KVC:遍歷字典中所有key,去模型中查找
* Runtime:根據(jù)模型中屬性名去字典中查找對應value,如果找到就給模型的屬性賦值.
*/
@implementation NSObject (Item)
// 字典轉模型
+ (instancetype)objectWithDict:(NSDictionary *)dict
{
// 創(chuàng)建對應模型對象
id objc = [[self alloc] init];
unsigned int count = 0;
// 1.獲取成員屬性數(shù)組
Ivar *ivarList = class_copyIvarList(self, &count);
// 2.遍歷所有的成員屬性名,一個一個去字典中取出對應的value給模型屬性賦值
for (int i = 0; i < count; i++) {
// 2.1 獲取成員屬性
Ivar ivar = ivarList[i];
// 2.2 獲取成員屬性名 C -> OC 字符串
NSString *ivarName = [NSString stringWithUTF8String:ivar_getName(ivar)];
// 2.3 _成員屬性名 => 字典key
NSString *key = [ivarName substringFromIndex:1];
// 2.4 去字典中取出對應value給模型屬性賦值
id value = dict[key];
// 獲取成員屬性類型
NSString *ivarType = [NSString stringWithUTF8String:ivar_getTypeEncoding(ivar)];
// 二級轉換,字典中還有字典,也需要把對應字典轉換成模型
//
// 判斷下value,是不是字典
if ([value isKindOfClass:[NSDictionary class]] && ![ivarType containsString:@"NS"]) { // 是字典對象,并且屬性名對應類型是自定義類型
// user User
// 處理類型字符串 @\"User\" -> User
ivarType = [ivarType stringByReplacingOccurrencesOfString:@"@" withString:@""];
ivarType = [ivarType stringByReplacingOccurrencesOfString:@"\"" withString:@""];
// 自定義對象,并且值是字典
// value:user字典 -> User模型
// 獲取模型(user)類對象
Class modalClass = NSClassFromString(ivarType);
// 字典轉模型
if (modalClass) {
// 字典轉模型 user
value = [modalClass objectWithDict:value];
}
// 字典,user
// NSLog(@"%@",key);
}
// 三級轉換:NSArray中也是字典,把數(shù)組中的字典轉換成模型.
// 判斷值是否是數(shù)組
if ([value isKindOfClass:[NSArray class]]) {
// 判斷對應類有沒有實現(xiàn)字典數(shù)組轉模型數(shù)組的協(xié)議
if ([self respondsToSelector:@selector(arrayContainModelClass)]) {
// 轉換成id類型禾酱,就能調(diào)用任何對象的方法
id idSelf = self;
// 獲取數(shù)組中字典對應的模型
NSString *type = [idSelf arrayContainModelClass][key];
// 生成模型
Class classModel = NSClassFromString(type);
NSMutableArray *arrM = [NSMutableArray array];
// 遍歷字典數(shù)組微酬,生成模型數(shù)組
for (NSDictionary *dict in value) {
// 字典轉模型
id model = [classModel objectWithDict:dict];
[arrM addObject:model];
}
// 把模型數(shù)組賦值給value
value = arrM;
}
}
// 2.5 KVC字典轉模型
if (value) {
[objc setValue:value forKey:key];
}
}
// 返回對象
return objc;
}
@end
#import <Foundation/Foundation.h>
#import "NSObject+Item.h"
@class User;
@interface Status : NSObject <ModelDelegate>
@property (nonatomic, strong) NSString *source;
@property (nonatomic, assign) int reposts_count;
@property (nonatomic, strong) NSArray *pic_urls;
@property (nonatomic, strong) NSString *created_at;
@property (nonatomic, assign) int attitudes_count;
@property (nonatomic, strong) NSString *idstr;
@property (nonatomic, strong) NSString *text;
@property (nonatomic, assign) int comments_count;
@property (nonatomic, strong) User *user;
@end
#import "Status.h"
@implementation Status
+ (NSDictionary *)arrayContainModelClass
{
return @{@"pic_urls" : @"Picture"};
}
@end