把请求的数据存放到Model中,(数组和和单条数据)
1、Model存放到数组中
#import <Foundation/Foundation.h> @interface VideoPinglunModel : NSObject @property (nonatomic , assign)NSInteger commentUserid; @property (nonatomic , assign)NSInteger commentCtime; @property (nonatomic , copy)NSString *commentContent; @property (nonatomic , copy)NSString *commentAuthor; - (instancetype)initWithDictionary:(NSDictionary *)dic; + (NSMutableArray *)modelSformDics:(NSArray *)arr; @end
- (void)setValue:(id)value forUndefinedKey:(NSString *)key { // if ([key isEqualToString:@"id"]) { // self.idM = value; // } } - (id)valueForUndefinedKey:(NSString *)key { return nil; } - (instancetype)initWithDictionary:(NSDictionary *)dic { self = [super init]; if (self) { [self setValuesForKeysWithDictionary:dic]; } return self; } + (NSMutableArray *)modelSformDics:(NSArray *)arr { NSMutableArray *modelArr = [NSMutableArray array]; for (NSDictionary *dic in arr) { VideoPinglunModel *model = [[self alloc] initWithDictionary:dic]; [modelArr addObject:model]; } return modelArr; }
使用方法;
//从解析的数组中查出需要的数据,存放到类的字典里面
NSMutableDictionary *dic = [responseObject objectForKey:@"data"];
playingModel *model = [[playingModel alloc] initWithDictionary:dic];
2、Model存放到中字典中(数据存到Model中)
#import <Foundation/Foundation.h> @interface playingModel : NSObject @property (nonatomic , copy)NSString *t; //标题吧 。。。 @property (nonatomic , copy)NSString *tag; @property (nonatomic , copy)NSString *desc; @property (nonatomic , copy)NSString *picpath; //图片地址 @property (nonatomic , copy)NSString *bigpicpath; @property (nonatomic , assign)NSInteger vtime; @property (nonatomic , copy)NSString *f; //视频播放地址 - (instancetype)initWithDictionary:(NSDictionary *)dic; + (instancetype)modelSformDics:(NSDictionary *)dic; @end
#import "playingModel.h" @implementation playingModel - (void)setValue:(id)value forUndefinedKey:(NSString *)key { } - (id)valueForUndefinedKey:(NSString *)key { return nil; } - (instancetype)initWithDictionary:(NSDictionary *)dic { self = [super init]; if (self) { [self setValuesForKeysWithDictionary:dic]; } return self; } + (instancetype)modelSformDics:(NSDictionary *)dic; { playingModel *model = [[self alloc] initWithDictionary:dic]; return model; } @end
/***************
****记住了*****
**************/
//判断这个对象不为空的时候不为空的时候
if (![[[responseObject objectForKey:@"data"] objectForKey:@"list"] isKindOfClass:[NSNull class]]) {
NSMutableArray *arr = [[responseObject objectForKey:@"data"] objectForKey:@"list"];
self.arrayComment = [VideoPinglunModel modelSformDics:arr];
}
//字典编辑 +(id)getAUsefulInstanceWith:(NSDictionary *)attributes key:(NSString *)key{ if ([[attributes objectForKey:key] isKindOfClass:[NSNumber class]] || [[attributes objectForKey:key] isKindOfClass:[NSString class]] || [[attributes objectForKey:key] isKindOfClass:[NSObject class]] || ![[attributes objectForKey:key] isKindOfClass:[NSNull class]]) { return [NSMutableString stringWithFormat:@"%@", [attributes objectForKey:key]]; } else { return @""; NSLog(@"字段值Id读取异常(字段不存在或者值为空)"); } }
本文出自 “小刘_Blog” 博客,转载请与作者联系!
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。