在我所做的項目中,不可缺的一個第三方庫-----MJExtension,它可以非常方便地用來構(gòu)造我們項目的模型層。
我們首先會創(chuàng)建一個自己想要的模型類,然后在.h中填充屬性,如果是兩三個,問題不大,但是像商品詳情啊這類的數(shù)據(jù)塊都是10個屬性左右的,就要耗費比較多的時間了,作為程序員,我們要優(yōu)雅地實現(xiàn)這些。
代碼獻上:
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)
// 定義一個關(guān)聯(lián)的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......");
// 異步獲取數(shù)據(jù)
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]) {
// 如果找到了相應(yīng)的key,遞歸就可以結(jié)束了
id tem = dict[keyName];
// 獲取關(guān)聯(lián)值
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]]) {
// 如果是數(shù)組,只取第0個數(shù)據(jù),并且傳值遞歸
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;
// 確定相應(yīng)值里面的元素,有哪些屬性
[dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
// 三元運算,先確定要替換值是否存在于請求返回的數(shù)據(jù),然后確定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
這樣就可以打印出屬性了,復(fù)制粘貼,就可以完成數(shù)據(jù)模型屬性的設(shè)置。當然這樣做的前提是后臺可以返回正確的數(shù)據(jù)。