响应和响应序列化程序 Swift 3.0 的 Alamofire 3->4 问题
Posted
技术标签:
【中文标题】响应和响应序列化程序 Swift 3.0 的 Alamofire 3->4 问题【英文标题】:Alamofire 3->4 trouble with Reponse & ResponseSerializer Swift 3.0 【发布时间】:2016-09-19 10:40:51 【问题描述】:我在使用 ResponseSerializer 时遇到问题,我得到一个未解析的标识符,而对于 Response,我得到一个未声明的类型。我从 alamofire migration doc 读到 Response 已更改为多种类型。所以我应该改变 Response->DataReponse 但这意味着我只能传递一个参数,比如:
// What I have
Response(<ListWrapper, NSError>)
// What I should change it to?
DataResponse(<ListWrapper>)
我怎样才能仍然以这种方式收到错误,更重要的是如何将扩展迁移到 alamofire 4?
我的班级:
class List
var idNumber: String?
var title: String?
var posterPath: String?
var release: String?
required init(json: JSON, id: Int?)
self.idNumber = json[ListFields.Id.rawValue].stringValue
self.title = json[ListFields.Title.rawValue].stringValue
self.posterPath = json[ListFields.PosterPath.rawValue].stringValue
self.release = json[ListFields.Release.rawValue].stringValue
class func setURL_APPEND(_ url: String)
URL_APPEND = url
// MARK: Endpoints
class func endpointForList() -> String
return URL_APPEND
fileprivate class func getListAtPath(_ path: String, completionHandler: @escaping (ListWrapper?, NSError?) -> Void)
Alamofire.request(path)
.responseListArray response in
if let error = response.result.error
completionHandler(nil, error)
return
completionHandler(response.result.value, nil)
class func getList(_ completionHandler: @escaping (ListWrapper?, NSError?) -> Void)
getListAtPath(List.endpointForList(), completionHandler: completionHandler)
// Problem is here:
// for ResponseSerializer I get an unresolved identifier
// and for Response I get an undeclared type
extension Alamofire.Request
func responseListArray(_ completionHandler: @escaping (Response<ListWrapper, NSError>) -> Void) -> Self
let responseSerializer = ResponseSerializer<ListWrapper, NSError> request, response, data, error in
guard error == nil else
return .failure(error!)
guard let responseData = data else
let failureReason = "Array could not be serialized because input data was nil."
let error = Alamofire.Error.errorWithCode(.dataSerializationFailed, failureReason: failureReason)
return .failure(error)
let JSONResponseSerializer = Request.JSONResponseSerializer(options: .allowFragments)
let result = JSONResponseSerializer.serializeResponse(request, response, responseData, error)
switch result
case .success(let value):
let json = SwiftyJSON3.JSON(value)
let wrapper = ListWrapper()
var allList:Array = Array<List>()
wrapper.totalCount = json["favorite_count"].intValue
// print(json)
let results = json["items"]
// print(results)
for jsonList in results
//print(jsonList.1)
let list = List(json: jsonList.1, id: Int(jsonList.0) )
if (list.posterPath == "")
continue
else
//print(movies.posterPath)
allList.append(list)
wrapper.results = allList
return .success(wrapper)
case .failure(let error):
return .failure(error)
return response(responseSerializer: responseSerializer,completionHandler: completionHandler)
【问题讨论】:
【参考方案1】:兄弟试试下面的代码看看:
func responseListArray(_ completionHandler: @escaping (Response<ListWrapper>) -> Void) -> Self
let responseSerializer = ResponseSerializer<ListWrapper> request, response, data, error in
guard error == nil else
return .failure(error!)
guard let responseData = data else
return .failure(AFError.responseSerializationFailed(reason: .inputDataNil))
let JSONResponseSerializer = Request.JSONResponseSerializer(options: .allowFragments)
let result = JSONResponseSerializer.serializeResponse(request, response, responseData, error)
switch result
case .success(let value):
let json = SwiftyJSON3.JSON(value)
let wrapper = ListWrapper()
var allList:Array = Array<List>()
wrapper.totalCount = json["favorite_count"].intValue
// print(json)
let results = json["items"]
// print(results)
for jsonList in results
//print(jsonList.1)
let list = List(json: jsonList.1, id: Int(jsonList.0) )
if (list.posterPath == "")
continue
else
//print(movies.posterPath)
allList.append(list)
wrapper.results = allList
return .success(wrapper)
case .failure(let error):
return .failure(error)
return response(responseSerializer: responseSerializer,completionHandler: completionHandler)
【讨论】:
我在最后一个返回语句“无法调用非函数类型 'NSHTTPURLResponse?' 的值”时遇到错误? 更改为扩展 Alamofire.DataRequest ?同样的问题? 是的,已经试过了,但没有用。无法识别 Response 和 ResponseSerializer。那么我不应该将它们也更改为 DataResponse 和 DataResponseSerializer 吗? 是的,将其更改为 DataResponse & DataResponseSerializer。现在怎么样了 修复了它。感谢您的帮助:)以上是关于响应和响应序列化程序 Swift 3.0 的 Alamofire 3->4 问题的主要内容,如果未能解决你的问题,请参考以下文章
UIViewController 内的 UITableView 没有响应(Swift 3.0)
swift 具有Argo和自定义错误类型的Generic Alamofire响应序列化程序
如何使用序列化为数据的响应数据处理程序(通过 AlamoFire 的 Swift 3)