Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type does not conform to protocol 'URLRequestConvertible' with Alamofire

Here's the code:

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

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.

like image 219
Bright Avatar asked Sep 23 '15 12:09

Bright


People also ask

What is URLRequestConvertible?

The URLRequestConvertible protocol is a lightweight way to ensure a given object can create a valid NSURLRequest . There's not really a strict set of rules or guidelines that exist forcing you to use this protocol in any particular way.

What is URLConvertible?

public protocol URLConvertible. Types adopting the URLConvertible protocol can be used to construct URL s, which can then be used to construct URLRequests . asURL() Returns a URL from the conforming instance or throws.


2 Answers

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


Update

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.

like image 77
cnoon Avatar answered Nov 09 '22 23:11

cnoon


@cnoon's answer works for version 3, but if you're using Alamofire version 4+, you will have to implement:

func asURLRequest() throws -> URLRequest

like image 35
static0886 Avatar answered Nov 09 '22 23:11

static0886