OC中模型類構(gòu)建屬性的打印(遞歸遍歷,支持無限分級,基于MJExtension)

在我所做的項目中,不可缺的一個第三方庫-----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ù)。

ZJPrintKey_github

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容