Swift:自定义Response
//调用的方法
让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:域名注册、网页空间、营销软件、网站建设、安次网站维护、网站推广。
func refresh(){
//如果有上拉加载更多正在执行,则取消它
ifself.tableView.mj_footer.isRefreshing() {
self.tableView.mj_footer.endRefreshing()
}
//根据 tab name获取帖子列表
TopicListModel.getTopicList(tab){
//自定义的Response
(response:V2ValueResponse<[TopicListModel]>) ->Voidin
if response.success {
//var topicList:Array
self.topicList = response.value
self.tableView.reloadData()
}
self.tableView.mj_header.endRefreshing()
}
}
/////////////////////////////////
//MARK: - Request
extensionTopicListModel {
/**
获取首页帖子列表
- parameter tab: tab名
*/
classfunc getTopicList(
_ tab:String? =nil ,
page:Int =0 ,
completionHandler:@escaping (V2ValueResponse<[TopicListModel]>) ->Void
)->Void{
var params:[String:String] = [:]
iflet tab = tab {
params["tab"]=tab
}
else {
params["tab"] ="all"
}
var url =V2EXURL
if params["tab"] =="all"&& page >0 {
params.removeAll()
params["p"] ="\(page)"
url =V2EXURL+"recent"
}
Alamofire.request(url, parameters: params, headers:MOBILE_CLIENT_HEADERS).responseJiHtml { (response) ->Voidin
var resultArray:[TopicListModel] = []
if let jiHtml = response.result.value{
iflet aRootNode = jiHtml.xPath("//body/div[@id='Wrapper']/div[@class='content']/div[@class='box']/div[@class='cell item']"){
for aNodein aRootNode {
let topic =TopicListModel(rootNode:aNode)
resultArray.append(topic);
}
}
DispatchQueue.global().async {
}
}
//在自定义的Response里面传值
let t =V2ValueResponse<[TopicListModel]>(value:resultArray, success: response.result.isSuccess)
completionHandler(t);
}
}
/////////////////////////////////
// V2Response.swift
import UIKit
class V2Response:NSObject {
var success:Bool =false
var message:String ="No message"
init(success:Bool,message:String?) {
super.init()
self.success = success
iflet message = message{
self.message = message
}
}
init(success:Bool) {
super.init()
self.success = success
}
}
class V2ValueResponse
var value:T?
overrideinit(success:Bool) {
super.init(success: success)
}
overrideinit(success:Bool,message:String?) {
super.init(success:success)
iflet message = message {
self.message = message
}
}
convenienceinit(value:T,success:Bool) {
self.init(success: success)
self.value = value
}
convenienceinit(value:T,success:Bool,message:String?) {
self.init(value:value,success:success)
iflet message = message {
self.message = message
}
}
}
网站标题:Swift:自定义Response
当前链接:http://myzitong.com/article/jdigjd.html