【问题标题】:Cannot convert value of type 'Response<AnyObject, NSError>' to closure result type 'NSDictionary'无法将“Response<AnyObject, NSError>”类型的值转换为闭包结果类型“NSDictionary”
【发布时间】:2016-08-28 03:26:35
【问题描述】:
class AlamofireService {


static let alamofireService = AlamofireService()


private init() {

}

internal func makePostServiceRequest(url: String, parameters: AnyObject, completion: (inner: () throws -> NSDictionary) -> Void) -> Void{


    Alamofire.request(.POST, URL_BASE, parameters: [IOS_PARAMS : parameters], encoding:.JSON).validate()
        .responseJSON

        {
            response in switch response.result
            {
            case .Success(let JSON):
                print("Success with JSON: \(JSON)")


                let response = JSON as! NSDictionary
                print(response)

                completion(inner: { return response })



            case .Failure(let error):
                print("Request failed with error: \(error)")
                completion(inner: { throw error })

            }
    }
}



internal func makeGetServiceRequestTemplates(completion: (inner: () throws -> NSDictionary) -> Void) ->Void {

    Alamofire.request(.GET, URl_TEMPLATES).validate().responseJSON

        {
            response in switch response.result
            {
            case .Success(let JSON):
                print("Success with JSON: \(JSON)")



                if let array = JSON as? [Dictionary<String, AnyObject>] {

                    for var i=0; i < array.count; i++ {

                        let templates = Mapper<VQuizTemplateTo>().map(array[i])
                        print(templates)

                    }


                }
                completion(inner: { return response }) //error is on this line


            case .Failure(let error):
                print("Request failed with error: \(error)")
                completion(inner: { throw error })
            }

    }
}

两个相同的方法,第一个没有错误,第二个是我标记的地方。无法弄清楚出了什么问题,尤其是这种无意义的错误。 你能告诉我我做错了什么吗?

【问题讨论】:

  • 你遇到了什么错误?

标签: ios swift exception-handling response


【解决方案1】:

您返回的responseResponse&lt;AnyObject, NSError&gt; 类型,这就是您收到该错误的原因..

尝试返回这个 response.result.value as! NSDictionary

只需将 completion(inner: { return response }) 替换为

completion(inner: { return response.result.value as! NSDictionary })

【讨论】:

  • 非常感谢,它有效!第一个 Alamofire 请求没有错误,仍然不明白这怎么可能,但这很重要
  • 请求中没有错误,但您正在尝试将 Response&lt;AnyObject, NSError&gt; 类型的值分配给 NSDictionary,这就是您遇到错误的原因。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-16
  • 2021-03-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多