类型与Alamofire不符合协议“ URLRequestConvertible” [英] Type does not conform to protocol 'URLRequestConvertible' with Alamofire

查看:253
本文介绍了类型与Alamofire不符合协议“ URLRequestConvertible”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是代码:

enum Router: URLRequestConvertible {
    //Error: Type 'Five100px.Router' does not conform to protocol 'URLRequestConvertible'

    static let baseURLString = "https://api.500px.com/v1"
    static let consumerKey = "MY_KEY"

    case PopularPhotos(Int)
    case PhotoInfo(Int, ImageSize)
    case Comments(Int, Int)

    var URLRequest: NSURLRequest {

        let (path, parameters) : (String, [String: AnyObject]) = {

            switch self {

            case .PopularPhotos(let page):
                let params = ["consumer_key": Router.consumerKey, "page": "\(page)", "feature": "popular", "rpp": "50", "include_store": "store_download", "include_status": "votes"]
                return ("/phtos", params)

            case .PhotoInfo(let photoID, let ImageSize):
                var params = ["consumer_key": Router.consumerKey, "image_size": "\(ImageSize.rawValue)"]
                return ("/photos/\(photoID)", params)

            case .Comments(let photoID, let commentsPage):
                var params = ["consumer_key": Router.consumerKey, "comments": "1", "comments_page": "\(commentsPage)"]
                return ("/photos/\(photoID)/comments", params)
            }
        }()

        let URL = NSURL(string: Router.baseURLString)
        let URLRequest = NSURLRequest(URL: URL!.URLByAppendingPathComponent(path))
        let encoding = Alamofire.ParameterEncoding.URL

        return encoding.encode(URLRequest, parameters: parameters).0
    }
}

我导入了Alamofire并添加了此代码,然后出现错误。我根据raywenderlich教程编写了此代码: http://www.raywenderlich.com/85080/beginning-alamofire-tutorial ,它是在我使用Swift 2时用Swift 1.2编写的。

I imported Alamofire and added this code, then comes the error. I wrote this code according to raywenderlich tutorial: http://www.raywenderlich.com/85080/beginning-alamofire-tutorial, which is written in Swift 1.2 while I use Swift 2.

推荐答案

您需要返回<$ c URLRequest 属性中的$ c> NSMutableURLRequest 而不是 NSURLRequest

You need to return an NSMutableURLRequest in the URLRequest property instead of an NSURLRequest. That will fix up the error.

在Swift 3和Alamofire 4,您需要从新的 asURLRequest()方法返回 URLRequest 。有关更多详细信息,请参阅我们更详细的自述文件示例

In Swift 3 and Alamofire 4, you need to return a URLRequest from the new asURLRequest() method. For more details, please refer to our much more detailed README examples.

这篇关于类型与Alamofire不符合协议“ URLRequestConvertible”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆