以下是代码:
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编写。
答案 0 :(得分:10)
您需要在NSMutableURLRequest
属性中返回URLRequest
而不是NSURLRequest
。这将解决错误。
在Swift 3和Alamofire 4中,您需要从新的URLRequest
方法返回asURLRequest()
。有关详细信息,请参阅我们更详细的自述文件examples。
答案 1 :(得分:0)
@ cnoon的答案适用于版本3,但如果您使用Alamofire版本4+,则必须实施:
func asURLRequest() throws -> URLRequest