Skip to content

Latest commit

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

学习 Alamofire封装库Moya

##Moya moya 对Alamofire网络请求库进行了封装,开发不需要写网络模型,管理等。使代码更加简洁。详情访问 Moya 项目主页

##1. 创建一个iOS Swift项目 (废话) 这里使用cocoapods 创建Podfile文件放在项目根目录下

source'https://github.com/CocoaPods/Specs.git'
use_frameworks!
pod 'Moya'

在项目根目录运行

pod install --verbose --no-repo-update // 不更新repo, 速度会快点

安装成功 点击.xcworkspace文件。启动项目

2. 创建API管理文件 DSAPI.Swift

这里我们使用逗视的api 1.引入Moya头文件

import Foundation
import Moya

2.创建DS的请求方法列表 enum

/**
MARK: 创建DS 请求方法列表
- GetVideosByType 根据类型获取视频列表 */
publicenumDS{case GetVideosByType(Int,Int,Int,Int) //根据类型获取视频列表
}

3.创建 MoyaProvider

// MARK: - Provider setup
letDSProvider=MoyaProvider<DS>()

4.通过Moya 定义的baseUrl、 请求路径(path)、 Http方法、 参数和目标的示例数据的协议。 扩展DS : TargetType

extensionDS:TargetType{
// 逗视API地址
publicvarbaseURL:NSURL{returnNSURL(string:"https://api.doushi.me/v1/rest/video/")! }
/// 拼接请求字符串
publicvarpath:String{switchself{case.GetVideosByType(let vid,let count,let type,let userId):return("getVideosByType/\(vid)/\(count)/\(type)/\(userId)")}}
/// 请求方法
publicvarmethod:Moya.Method{return.GET
}
/// 配置参数
publicvarparameters:[String:AnyObject]?{switchself{default:returnnil}}
/// 数据
publicvarsampleData:NSData{switchself{case.GetVideosByType:return"Half measures are as bad as nothing at all.".dataUsingEncoding(NSUTF8StringEncoding)!
}}}

OK 这样创建好了DSAPI管理类,下面在tableView中实验一把

5.创建VideosTableViewController

  • 定义数据集合
varrepos=NSArray()
  • 创建loadData()方法请求网络数据
/**
请求数据
*/
func loadData(){DSProvider.request(DS.GetVideosByType(0,12,0,1)){(result)->()inswitch result {caselet.Success(response):do{letjson:Dictionary?=try response.mapJSON()as?Dictionary<String,AnyObject>iflet json = json {print(json["content"]as!Array<AnyObject>)iflet contents:Array<AnyObject>=json["content"]as?Array<AnyObject>{self.repos = contents
}}self.tableView.reloadData()}catch{}caselet.Failure(error):print(error)break}}}
  • 扩展VideosTableViewController Cell增添数据
extensionVideosTableViewController{overridefunc tableView(tableView:UITableView, cellForRowAtIndexPath indexPath:NSIndexPath)->UITableViewCell{letcell= tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath)
// Configure the cell...
letcontentDic=repos[indexPath.row]as?Dictionary<String,AnyObject>
cell.textLabel?.text = contentDic!["title"]as?Stringreturn cell
}overridefunc tableView(tableView:UITableView, numberOfRowsInSection section:Int)->Int{return repos.count
}}

OK,点击Run看一下效果吧

正确获取到 逗视app的视频列表了


有没有发现这样写网络请求很爽,我们只维护DSAPI就可以了,添加请求方法,处理数据等。 本文例子已经上传到github,有问题可以发送邮件iosdev#itjh.com.cn

About

学习Moya

Resources

Stars

17 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages