如何将Alamofire路由器类迁移到Swift 3? [英] How to migrate Alamofire router class to Swift 3?

查看:178
本文介绍了如何将Alamofire路由器类迁移到Swift 3?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道如何改变Swift 3的整个方法?此时我有一些非常类似于Swift 2.2的工作正常但现在我正在尝试将其更改为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 now I'm trying to change that to Swift 3.

我收到了一些错误URLRequestConvertible,使用Alamofire.Method(我改为HTTPMethod,现在正在工作)以及参数编码,除此之外我还没有遵守整个协议。

I am 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.

我正在等待Alamofire工程师的指导,但我希望看到我能在此期间完成的工作。

I'm waiting for guidance from engineers at Alamofire, but I am looking to see what I can accomplish in the meantime.

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


推荐答案

为Alamofire 4.0.0版本编辑(更新 URLRequestConvertible 具有投掷功能的协议):



很多在Swift 3中发生了变化,你应该先重新开始y阅读所有更改,可以从 http://swift.org 开始。这是固定代码:

EDITED for Alamofire 4.0.0 release (updated URLRequestConvertible protocol with throwing capabilities):

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

    func asURLRequest() throws -> URLRequest {
        let url = URL(string: Router.baseURLString)!
        var urlRequest = URLRequest(url: url.appendingPathComponent(path))
        urlRequest.httpMethod = method.rawValue

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

        switch self {
        case .createUser(let parameters):
            return try Alamofire.JSONEncoding.default.encode(urlRequest, with: parameters)
        case .updateUser(_, let parameters):
            return try Alamofire.URLEncoding.default.encode(urlRequest, with: parameters)
        default:
            return urlRequest
        }
    }
}

Swift 3的主要变化是:

The main changes for Swift 3 are :


  • enum 案例现在是小写的,你也应该采用它。

  • 变量名现在以小写开头,即使它是URL之类的缩写。这就是为什么协议需要 var urlRequest 而不是 var URLRequest (这会与下一点发生冲突)

  • 再见 NS 许多地方都有前缀。 NSURLRequest NSMutableURLRequest 现在 URLRequest NSURL URL 等。

  • 你如何命名你的函数和参数现在很多更少冗余和更自然。例如,参见 URLByAppendingPathComponent 如何更改。

  • enum cases are now lowercase and you should adopt it too.
  • Variable names now start with lowercase, even if it's an abbreviation like "URL". That why the protocol requires var urlRequest and not var URLRequest (and it would conflict with the next point)
  • 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.

和Alamofire v4一样:

And as for Alamofire v4 :


  • 有一个新的 ParameterEncoding 协议自己编码参数不同但功能更多

  • 这里没有任何影响的其他更改,但你也一定要阅读它们。

  • 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.

最后的建议:如果时间敏感,请避免迁移到未发布的编程语言或API版本。 Swift 3不会让步,但Alamofire仍然可能!例如 ParameterEncoding 协议只有两天了! (编辑:事实上它已经改变了,现在在上面的最终版本中)

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! ( and indeed it changed since, now in its final version above)

干杯

这篇关于如何将Alamofire路由器类迁移到Swift 3?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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