(_, _, _) -> Void' is not convertible to 'Response<AnyObject, NSError> -> Void'

一世执手 提交于 2019-12-25 06:07:46

问题


I imported Alamofire the way I used to successfully import it, and when I had this code, there comes an error:

Alamofire.request(.GET, postEndPoint).responseJSON {(request, response, result) in
//Error: '(_, _, _) -> Void' is not convertible to 'Response<AnyObject, NSError> -> Void'

        guard let value = result.value else {
            print("Error: did not receive data")
            return
        }

        guard result.error == nil else {
            print("error calling GET on /posts/1")
            print(result.error)
            return
        }

        let post = JSON(value)

        print("The post is: " + post.description)
        if let title = post["title"].String {
            print("The title is: " + title)
        } else {
            print("Error parsing /posts/1")
        }
}

I didn't use CocoaPods for Alamofire.


回答1:


See the Alamofire 3.0 Migration Guide.

Alamofire.request(.GET, postEndPoint).responseJSON { response in
    if let JSON = response.result.value {
        print("JSON: \(JSON)")
    }
}

This is the new way of getting the JSON.


response.result is the new way of getting result.

Alamofire.request(.GET, postEndPoint).responseJSON { response in
    guard let value = response.result.value else {
        print("Error: did not receive data")
        return
    }

    guard response.result.error == nil else {
        print("error calling GET on /posts/1")
        print(response.result.error)
        return
    }

    let post = JSON(value)

    print("The post is: " + post.description)
    if let title = post["title"].String {
        print("The title is: " + title)
    } else {
        print("Error parsing /posts/1")
    }
}


来源:https://stackoverflow.com/questions/32960808/void-is-not-convertible-to-responseanyobject-nserror-void

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!