把请求的数据存放到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读取异常(字段不存在或者值为空)");
    }
}
时间: 2024-07-28 14:34:11

把请求的数据存放到Model中,(数组和和单条数据)的相关文章

把请求的数据(数组和和单条数据)存放到Model中

#import <Foundation/Foundation.h> @interface VideoPinglunModel : NSObject @property (nonatomic , assign)NSInteger commentUserid; @property (nonatomic , assign)NSInteger commentCtime; @property (nonatomic , copy)NSString *commentContent; @property (n

获取项目中文件,存放到Debug中。

说起这个,还真是费了一般功夫. 说个最简单的方法: 第一步:把需要生成到Debug中的文件放到项目中(注意:当前文件夹目录是什么样的,存放到Debug中也是什么样) 第二部:设置文件属性中 复制到输出目录(如果较新则复制:就是在内容更改后就更新,不复制:不会复制,始终复制:如果该文件需要修改,不建议选择此项) 生成操作(无,编译,内容,嵌入的资源) 第三步:已经完成了,是不是很简单. 还有个手动的方法(哈哈,有点麻烦哦!!!): /// <summary> /// 项目中资源文件保存到Debu

javascript 从一组数据中找到指定的单条数据的方法 by FungLeo

从一组数据中找到指定的单条数据的方法 在一般情况下,我们会要求后端在列表的时候输出一堆列表的JSON数据给我们,然后我们把这堆数据循环,就能在前端上显示列表了. 而我们在内容页的时候,则要求输出一个内容页的JSON数据给我们,我们就可以做内容页了. 但是,有时候,数据并不是特别复杂,我们可能需要从列表的数据中指定其中的单条数据.怎么做呢? 标准答案,find方法 var json = [{"id":1,"name":"张三"},{"id

excel表格中的数据如何放到数据库中(非导入)

平时经常用到excel中的数据,想比较两个excel数据的关联性,数据太多,所以放到数据库中,用脚本查询就会比较方便.这里说一下怎么放到数据库中并查询对比. 这里以PL/SQL为例. 需求:有两张表,其中每个表中有一列与另一表的列相同,如何让他们一一对应.如图两个excel 第二张 红框内的数据为相等的数据,现在想查看一一对应关系 方法是: 1.在数据库中建表asad_t1 对应excel1,并多出一个字段,表2asad_t2对应excel2 2.将excel1中数据复制,在plsql中执行se

Spring将classpath下的 .properties文件数据读出放到map中,在初始化时加载

因为项目需要需要将配置文件中的键值对读出放到map中 格式为: 001=123456789 Appcontext.xml中添加配置: <bean id="loadKeyFromProperties" class="com.:landau.init.LoadKeyFormProperties"> <property name="keyFileResource"> <value>classpath:keys.pro

requests从api中获取数据并存放到mysql中

python的requests库是一个非常强大的库,requests的安装方法十分简单,用: pip install requests 即可安装requests,安装成功后: import requests 即可导入requests模块,requests有get和post两种方法: 1.requests.get()用法: url = "http://xxx" a_content = requests.get(url) aa = a_content.content #.content和.

shell将sql查询结果存放到excel中

#!/bin/shcd /d/compareMYSQL="mysql -h      -uroot -p**** --default-character-set=utf8 -A";sql="select * from contrast.dx_record limit 0,10 ";$MYSQL -e "$sql" >dx.xlsecho "end"; 转换编码的指令 iconv -f utf-8 -t gb2312 dx

关于将16进制的数据存放在char数组中的问题的解决方案

首先说明一点,题目提到的问题我目前并没有解决,我在此处发表随笔就是希望看到的同学们有会的帮我解决一下. 现在来说一下我的需求: 1.定义一个数组:unsigned char cmd[6] = {0xF0,0x11,0x01,0x65,0x87,0xA9}; 2.然后将这个数组的数据写入串口中,我在写的时候发现了cmd中的数据出现了乱码的现象,于是我采用下面一种方法: 3.使用sprintf函数,于是有了下面的代码: 1 char pBuf[9]; 2 sprintf(pBuf,"%x %x %x

怎样简单灵活地将DataTable中的数据赋值给model

最近在做的一个项目中,有13个方法都需要用到同一种处理方式:通过SQL语句从数据库获取一条指定的数据,并将该数据中的每个值都赋值给一个model,再将这个model中的数据通过微信发送出去.每个方法都会从不同的表中获取数据,需要处理的数据完全不同,因此也对应了13个不同的model.这些model中的每个属性名称是已经给定的,需要将它们post出去,使用微信给定的模板发送消息,因此每个属性的名称必须与微信提供的模板中一致. 下面举个简单的例子来说明当前的情况: SQL Server中用来存放数据