请求解析数据 SDWebImage请求图片

来源:互联网 发布:数据库系统工程师希赛 编辑:程序博客网 时间:2024/06/10 00:37

                              请求解析数据

一般解析数据常用Json解析方法
固定的模式如下:
                   
    NSURL * url = [NSURL URLWithString:@"网址"];    NSURLSession * session = [NSURLSession sharedSession];    NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url];    NSURLSessionTask  * task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {            }];    [task resume];}
以音乐播放器为例
 NSURL * url = [NSURL URLWithString:string];   // 用JSON解析工具发现数据是一个数组 所以直接从网址获取数据存到一个数组   NSArray *array = [NSArray arrayWithContentsOfURL:url];        NSLog(@"array.count=%ld",array.count);        // 对数组进行遍历        for (NSDictionary *dic in array) {            // 使用model类接收            MusicModel *m=[[MusicModel alloc]init];            [m setValuesForKeysWithDictionary:dic];            [self.dataArray addObject:m];        }    [self.tableView reloadData];// 重新载入数据
解析数据后用model接收
    MusicModel * m=self.dataArray[indexPath.row];    cell.nameLabel.text = m.name;    cell.singerLabel.text = m.singer;

加载图片的时候导入第三方(直接找个现成的拉进去)
[cell.imv sd_setImageWithURL:[NSURL URLWithString:m.picUrl] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {        m.image = image;    }];

想使用找个方法,要在单例里写个归档方法

// 用来记录被归档对象属性
- (void)encodeWithCoder:(NSCoder *)aCoder{    [aCoder encodeObject:self.image forKey:@"picUrl"];// 做标记}
// 重新获取归档对象的属性值- (id)initWithCoder:(NSCoder *)aDecoder{    self = [super init];    if (self) {        self.image = [aDecoder decodeObjectForKey:@"picUrl"];    }    return self;     }
 豆瓣的接口则是字典里面套数组,数组里面再套用个字典,而数据解析最根本的目的:找到一个相对最小的,数组套字典格式的数据。

    NSURL * url = [NSURL URLWithString:@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/activitylist.php"];    NSURLRequest * request = [NSURLRequest requestWithURL:url];    NSURLSession * session = [NSURLSession sharedSession];    NSURLSessionDataTask * task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {        // 用解析工具发现最外层是个字典,创建个字典接收        NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingAllowFragments) error:nil];        // 字典里还有数组        NSArray * array = dic[@"events"];                // 遍历数组        for (NSDictionary * dic1 in array) {            // 创建单例接收            News * n = [[News alloc] init];            // KVC赋值(虽然我也不知道这是什么东西)            [n setValuesForKeysWithDictionary:dic1];            // dataArray已经初始化过了(懒加载) 添加到数组            [self.dataArray addObject:n];            // 重新载入数据            [self.tableView reloadData];        }    }];        [task resume];      }
SDWebImage

在使用SDWebImage导入图片时需要导入第三方,最简单的方法就是直接拉一个过来

// 在自定义cell 上使用这个方法加载图片
// imv 是cell上得图片
// picUrl 在单例中定义的属性  表示image的字符串
   
<span style="font-size:18px;">[cell.imv sd_setImageWithURL:[NSURL URLWithString:m.image] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {           }];</span>





                  
0 0
原创粉丝点击