无法将'Response <anyobject,nserror =“”>'类型的值转换为闭包结果类型'NSDictionary'

时间:2016-08-28 03:26:36

标签: ios swift exception-handling 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 })
            }

    }
}

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

1 个答案:

答案 0 :(得分:1)

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

请尝试将其返回response.result.value as! NSDictionary

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

即可

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