首页 > 代码库 > 把请求的数据存放到Model中,(数组和和单条数据)
把请求的数据存放到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” 博客,转载请与作者联系!
把请求的数据存放到Model中,(数组和和单条数据)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。