2016 - 1 - 23 json转模型 常用的第三方框架

时间:2023-03-08 17:05:33

一: 三个常用的框架

1. Mantle

- 所有模型必须继承MTModel

2. JSONModel

- 所有模型必须继承JSONModel

3.MJExtension

- 不需要继承任何东西。

- 代码:

//
// TableViewController.m
// json
//
// Created by Mac on 16/1/23.
// Copyright © 2016年 Mac. All rights reserved.
// #import "TableViewController.h"
#import <UIImageView+WebCache.h>
#import <MediaPlayer/MediaPlayer.h>
#import "ZZVideo.h"
#import <MJExtension.h>
@interface TableViewController ()
@property (nonatomic, strong) NSArray *videos; @end @implementation TableViewController - (void)viewDidLoad {
[super viewDidLoad];
NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/video"]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]; self.videos = [ZZVideo mj_objectArrayWithKeyValuesArray:dic[@"videos"]]; // NSLog(@"%zd",_videos.count);
[self.tableView reloadData];
}]; } #pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//#warning Incomplete implementation, return the number of sections
return ;
} - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//#warning Incomplete implementation, return the number of rows
return self.videos.count;
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *ID = @"video"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; ZZVideo *video = self.videos[indexPath.row]; cell.detailTextLabel.text = [NSString stringWithFormat:@"视频时长: %ld",(long)video.length]; cell.textLabel.text= video.name; NSString *image = [@"http://120.25.226.186:32812" stringByAppendingPathComponent:video.image]; [cell.imageView sd_setImageWithURL:[NSURL URLWithString:image] placeholderImage:nil]; return cell;
}
#pragma mark - 代理
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
ZZVideo *video = self.videos[indexPath.row]; NSString *videoStr = [@"http://120.25.226.186:32812" stringByAppendingPathComponent:video.url]; MPMoviePlayerViewController *vc = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:videoStr]]; [self presentViewController:vc animated:YES completion:nil]; }
@end

二: 设计第三方框架需要考虑的地方

1.侵入性:

- 侵入性大就意味着项目很难离开这个框架

2.易用性

- 比如少量代码就可以实现很多功能

3.扩展性

- 很容易给框架增加新的功能