在我所做的项目中,不可缺的一个第三方库-----MJExtension,它可以非常方便地用来构造我们项目的模型层。
我们首先会创建一个自己想要的模型类,然后在.h中填充属性,如果是两三个,问题不大,但是像商品详情啊这类的数据块都是10个属性左右的,就要耗费比较多的时间了,作为程序员,我们要优雅地实现这些。
代码献上:
NSObject+ZJPrintKey.h
#import
@interface NSObject (ZJPrintKey)
/**
* 打印模型类的所有属性
*
* @param urlStr 请求URL
* @param key 要获取的值的key
* @param dict 要替换名字的属性,可以为nil
*/
- (void)zj_dictionaryToLogUrlStr:(NSString *)urlStr andKey:(NSString *)key andKeyReplaceDictionary:(NSDictionary *)dict;
@end
NSObject+ZJPrintKey.m
#import "NSObject+ZJPrintKey.h"
#import
@implementation NSObject (ZJPrintKey)
// 定义一个关联的key
static char replaceDictionaryKey;
- (void)zj_dictionaryToLogUrlStr:(NSString *)urlStr andKey:(NSString *)key andKeyReplaceDictionary:(NSDictionary *)dict {
if (!urlStr || urlStr.length == 0 || !key) {
return;
}
objc_setAssociatedObject(self, &replaceDictionaryKey, dict, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
NSLog(@"Loading......");
// 异步获取数据
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
NSData *data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:urlStr]];
NSLog(@"Loaded");
NSDictionary *temDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
[self findTheValue:key andDict:temDict];
});
}
- (void)findTheValue:(NSString *)str andDict:(NSDictionary *)dict {
NSArray *ak = dict.allKeys;
for (NSString *keyName in ak) {
if ([keyName isEqualToString:str]) {
// 如果找到了相应的key,递归就可以结束了
id tem = dict[keyName];
// 获取关联值
NSDictionary *temDict = objc_getAssociatedObject(self, &replaceDictionaryKey);
if ([tem isKindOfClass:[NSDictionary class]]) {
[self handleDict:tem andKeyreplaces:temDict];
}
else if ([tem isKindOfClass:[NSArray class]]) {
if ([tem count] >= 1) {
[self handleDict:tem[0] andKeyreplaces:temDict];
}
}
else {
NSLog(@"Format is not correct");
}
objc_setAssociatedObject(self, &replaceDictionaryKey, nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
return;
}
else {
id tem = dict[keyName];
if ([tem isKindOfClass:[NSDictionary class]]) {
// 递归,遍历下一层,如果是字典的话
[self findTheValue:str andDict:tem];
}
else if ([tem isKindOfClass:[NSArray class]]) {
// 如果是数组,只取第0个数据,并且传值递归
if ([tem count] >= 1) {
[self findTheValue:str andDict:tem[0]];
}
}
else {
// 其他
}
}
}
NSLog(@"not found");
}
- (void)handleDict:(NSDictionary *)dict andKeyreplaces:(NSDictionary *)kDict {
NSMutableString *mustr = [NSMutableString new];
__block NSDictionary *temDict = kDict;
// 确定相应值里面的元素,有哪些属性
[dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
// 三元运算,先确定要替换值是否存在于请求返回的数据,然后确定temDict不为nil
NSString *temKey = temDict ? (temDict[key] ? temDict[key] : key) : key;
if ([obj isKindOfClass:[NSNumber class]]) {
[mustr appendString:[NSString stringWithFormat:@"@property (strong, nonatomic) NSNumber *%@;\n", temKey]];
}
else if ([obj isKindOfClass:[NSArray class]]) {
[mustr appendString:[NSString stringWithFormat:@"@property (strong, nonatomic) NSArray *%@;\n", temKey]];
}
else {
[mustr appendString:[NSString stringWithFormat:@"@property (copy, nonatomic) NSString *%@;\n", temKey]];
}
}];
NSLog(@"\n/**********ZJPrintKey***********/\n%@/**********ZJPrintKey***********/\n", mustr);
}
@end
这样就可以打印出属性了,复制粘贴,就可以完成数据模型属性的设置。当然这样做的前提是后台可以返回正确的数据。
ZJPrintKey_github