ios – Type与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 1.2编写的,而我使用的是Swift 2.

解决方法

您需要在URLRequest属性中返回NSMutableuRLRequest而不是NSURLRequest.这将解决错误.

更新

在Swift 3和Alamofire 4中,您需要从新的asURLRequest()方法返回URLRequest.有关详细信息,请参阅我们更详细的README examples.

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...