美文网首页
OC中模型类构建属性的打印(递归遍历,支持无限分级,基于MJEx

OC中模型类构建属性的打印(递归遍历,支持无限分级,基于MJEx

作者: 841883f31e69 | 来源:发表于2016-09-05 15:11 被阅读125次

在我所做的项目中,不可缺的一个第三方库-----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

相关文章

  • OC中模型类构建属性的打印(递归遍历,支持无限分级,基于MJEx

    在我所做的项目中,不可缺的一个第三方库-----MJExtension,它可以非常方便地用来构造我们项目的模型层。...

  • 树的遍历

    节点结构: 先序遍历 递归 非递归 后序遍历 递归 非递归 中序遍历 递归 非递归 层序遍历 类库 有了上述遍历算...

  • 二叉树遍历java,非递归、层次。

    /** * 前序遍历 * 递归 */ /*** 前序遍历* 非递归*/ 后续遍历非递归 二叉树层次遍历基于java...

  • MJExtension 源码浅析

    OC 项目中流行的字典转模型框架 ,出自CoderMJLee的开源框架 MJExtension 最近在阅读MJEx...

  • day1

    oc对象读取属性值的几种方法 遍历类所有属性名称 遍历集合的几种方式 协议(Protocol)类似于java的in...

  • OC与Swift中的 description

    Swift 中重写父类description方法,输出模型中的内容 OC中 在自定义的类中,将其中的属性在desc...

  • 简析iOS开发中使用模型的好处

    iOS开发中的模型指的是数据模型,是用来存放数据的对象。在OC中,模型的建立是基于NSObject的(即为一个类)...

  • 二叉树遍历

    先序遍历——[递归、非递归] 中序遍历——[递归、非递归] 后序遍历——[递归、非递归] 层次遍历——[递归、非递归]

  • Effective Objective-C 2.0笔记(二)

    对象,消息,运行期 理解“属性”这一概念 OC是通过运行时机制来提供相关支持的,属性则是用来封装OC对象中数据的类...

  • python遍历二叉树

    定义二叉树: 构建二叉树: BFS: 先序遍历:1.递归版本: 2.非递归版本: 中序遍历: 1.递归版本 2.非...

网友评论

      本文标题:OC中模型类构建属性的打印(递归遍历,支持无限分级,基于MJEx

      本文链接:https://www.haomeiwen.com/subject/owprettx.html