(_,_,_) - &gt;虚空&#39;不能转换为&#39; Response <anyobject,nserror =“”> - &gt;虚空&#39;

时间:2015-10-06 02:30:25

标签: xcode swift alamofire

我按照以前成功导入Alamofire的方式导入了Alamofire,当我使用此代码时出现错误:

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

我没有将CocoaPods用于Alamofire。

1 个答案:

答案 0 :(得分:4)

请参阅Alamofire 3.0 Migration Guide

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

这是获取JSON的新方法。

response.result是获取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")
    }
}