Alamofire v4 使用未声明的类型响应
Posted
技术标签:
【中文标题】Alamofire v4 使用未声明的类型响应【英文标题】:Alamofire v4 use of undeclared type response 【发布时间】:2016-10-03 13:47:10 【问题描述】:当我更新到 Alamofire v4 时,我遇到了两个错误。
第一个是使用未声明的类型响应,我从这一行得到:
func responseSavedAdsArray(_ completionHandler: (Response<SavedAdsWrapper, NSError>) ->
第二个错误是 use of unresolved identifier ResponseSerializer 我从这一行得到的:
let responseSerializer = ResponseSerializer<SavedAdsWrapper, NSError> request, response, data, error in
这是我所有的代码:
extension Alamofire.Request
func responseSavedArray(_ completionHandler: (Response<SavedWrapper, NSError>) -> Void) -> Self
let responseSerializer = ResponseSerializer<SavedWrapper, 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 userInfo: Dictionary<NSObject, AnyObject> = [NSLocalizedFailureReasonErrorKey: failureReason, Error.UserInfoKeys.StatusCode: response!.statusCode]
let error = NSError(domain: Error.Domain, code: Error.Code.StatusCodeValidationFailed.rawValue, userInfo: userInfo)
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 = JSON(value)
let wrapper = SavedWrapper()
wrapper.next = json["saveditems"]["next_page_url"].stringValue
wrapper.previous = json["saveditems"]["prev_page_url"].stringValue
wrapper.count = json["saveditems"]["total"].intValue
var allSaved:Array = Array<Saved>()
print(json)
let results = json["saveditems"]["data"]
print(results)
for jsonAdata in results
let adsJData = SavedAds(json: jsonAdata.1, id: Int(jsonAdata.0))
allSaved.append(adsJData)
wrapper.saveditems = allSaved
return .Success(wrapper)
case .Failure(let error):
return .Failure(error)
return response(responseSerializer: responseSerializer,
completionHandler: completionHandler)
那是什么原因造成的呢?
【问题讨论】:
【参考方案1】:先改变
func responseSavedAdsArray(_ completionHandler: (Response<SavedAdsWrapper, NSError>) ->
到
func responseSavedAdsArray(_ completionHandler: (DataResponse<SavedAdsWrapper>) ->
已编辑
let responseSerializer = ResponseSerializer<SavedAdsWrapper, NSError> request, response, data, error in
到
let responseSerializer = DataResponseSerializer<SavedAdsWrapper> request, response, data, error in
【讨论】:
修复了第一个错误,但我仍然得到:使用未解析的标识符 ResponseSerializer 不也一样吗?试试 DataResponseSerializer以上是关于Alamofire v4 使用未声明的类型响应的主要内容,如果未能解决你的问题,请参考以下文章
var 请求:Alamofire.Request?使用未声明类型的 Alamofire
响应和响应序列化程序 Swift 3.0 的 Alamofire 3->4 问题
Response<AnyObject, NSError> 在 Alamofire4/Swift3 中未声明的类型