ios - 无法将类型 'Response<AnyObject, NSError>' 的值转换为闭包结果类型 'NSDictionary'

标签 ios swift exception response

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 })
            }

    }
}

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

最佳答案

response您返回的类型是 Response<AnyObject, NSError> ,这就是您收到该错误的原因..

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

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

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

关于ios - 无法将类型 'Response<AnyObject, NSError>' 的值转换为闭包结果类型 'NSDictionary',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39187757/

相关文章:

ruby-on-rails - 如何继续 Ruby 中的下一个救援 block ?

c++ - C++ 中的内存管理 : Creating objects in a loop without crashing the program

ios - 如何在 UITableviewCustomCell 中为文本字段添加边框颜色 - iPhone

ios - 隐藏 View 时,自动布局中的 Swift 约束不会改变

ios - Swift 4 中的可达性问题

java - 我如何知道 Kotlin 中的方法可能引发哪些异常 - Android

ios - 在 map 应用 iOS 上获取方向功能

ios - 快速转换为泛型类类型

coding-style - Swift 中的早期回归/黄金路径

ios - Swift:Google Place Picker,避免使用 map UI