Gabriel Candia Gabriel Candia - 2 months ago 97
Swift Question

Migrating Alamofire router class to Swift 3

Does anybody know how to change this entire approach to Swift 3?. At this moment i have something very similar to this working ok on Swift 2.2 but right now i'm trying to change that to Swift 3 and getting some errors with the "URLRequestConvertible", with the Alamofire.Method (that i changed to HTTPMethod and now is working) and also with the parameter encoding, besides that i'm not conforming the entire protocol. I'm waiting for the guide from the guys of Alamofire but i really need to make this changes asap.

Hope you can help me.

enum Router: URLRequestConvertible {
static let baseURLString = "http://example.com"
static var OAuthToken: String?

case CreateUser([String: AnyObject])
case ReadUser(String)
case UpdateUser(String, [String: AnyObject])
case DestroyUser(String)

var method: Alamofire.Method {
switch self {
case .CreateUser:
return .POST
case .ReadUser:
return .GET
case .UpdateUser:
return .PUT
case .DestroyUser:
return .DELETE
}
}

var path: String {
switch self {
case .CreateUser:
return "/users"
case .ReadUser(let username):
return "/users/\(username)"
case .UpdateUser(let username, _):
return "/users/\(username)"
case .DestroyUser(let username):
return "/users/\(username)"
}
}

// MARK: URLRequestConvertible

var URLRequest: NSMutableURLRequest {
let URL = NSURL(string: Router.baseURLString)!
let mutableURLRequest = NSMutableURLRequest(URL: URL.URLByAppendingPathComponent(path))
mutableURLRequest.HTTPMethod = method.rawValue

if let token = Router.OAuthToken {
mutableURLRequest.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
}

switch self {
case .CreateUser(let parameters):
return Alamofire.ParameterEncoding.JSON.encode(mutableURLRequest, parameters: parameters).0
case .UpdateUser(_, let parameters):
return Alamofire.ParameterEncoding.URL.encode(mutableURLRequest, parameters: parameters).0
default:
return mutableURLRequest
}
}
}

Answer

EDIT: A lot has changed in Swift 3 and you should first really read up on all the changes, maybe starting at http://swift.org. Here's the fixed code :

enum Router : URLRequestConvertible {
    static let baseURLString = "http://example.com"
    static var OAuthToken: String?

    case CreateUser([String: AnyObject])
    case ReadUser(String)
    case UpdateUser(String, [String: AnyObject])
    case DestroyUser(String)

    var method: Alamofire.HTTPMethod {
        switch self {
        case .CreateUser:
            return .post
        case .ReadUser:
            return .get
        case .UpdateUser:
            return .put
        case .DestroyUser:
            return .delete
        }
    }

    var path: String {
        switch self {
        case .CreateUser:
            return "/users"
        case .ReadUser(let username):
            return "/users/\(username)"
        case .UpdateUser(let username, _):
            return "/users/\(username)"
        case .DestroyUser(let username):
            return "/users/\(username)"
        }
    }

    var urlRequest: URLRequest {
        let url = URL(string: Router.baseURLString)!
        var mutableURLRequest = URLRequest(url: url.appendingPathComponent(path))
        mutableURLRequest.httpMethod = method.rawValue

        if let token = Router.OAuthToken {
            mutableURLRequest.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
        }

        switch self {
        case .CreateUser(let parameters):
            //return Alamofire.ParameterEncoding.json.encode(mutableURLRequest, parameters: parameters).0
            return try! Alamofire.JSONEncoding().encode(mutableURLRequest, with: parameters)
        case .UpdateUser(_, let parameters):
            return try! Alamofire.URLEncoding().encode(mutableURLRequest, with: parameters)
        default:
            return mutableURLRequest
        }
    }
}

The main changes for Swift 3 are :

  • enum cases are now lowercase and you should adopt it too.
  • Variables names now start with lowercase, even if it's an abbreviation like "URL".
  • Bye-bye NS prefix in many places. NSURLRequest and NSMutableURLRequest are now URLRequest, NSURL is URL, etc.
  • How you name your functions and parameters is now a lot less redundant and more natural. See for example how URLByAppendingPathComponent has changed.

And as for Alamofire v4 :

  • There's a new ParameterEncoding protocol to encoding parameters yourself is different but more versatile
  • MANY other changes which have no impact here but you sure have to read about them too.

And final word of advice : avoid migrating to unreleased versions of a programming language or API if it's time-sensitive. Swift 3 won't budge much but Alamofire still might! For example the ParameterEncoding protocol is only two days old!

Cheers

Comments