有一些问题与Alamofire迁移到swift 3

时间:2017-08-02 12:08:13

标签: swift swift3 alamofire

从我那里得到一些错误,将这个项目迁移到了Alamofire的swift 3。第二行是我收到错误的地方,有人可以帮我修复这段代码。看到错误我在下面

import Foundation
import Alamofire

struct ApiHandler {

    enum Router: URLRequestConvertible {
        static let baseURLString = "example.com"
        static let accessToken = "4kjn1kjb3213jkb12k3b12kn321k00"

        case categories(String)
        case assets
        case article(String)
        case top10(String)

        var URLRequest: NSMutableURLRequest {
            let (path, parameters) : (String, [String: AnyObject]?) = {
                switch self {
                case .categories(let type):
                    let params = ["access_token": "\(Router.accessToken)", "content_type": "\(type)"]
                    return ("/entries", params as [String : AnyObject])
                case .assets:
                    let params = ["access_token": "\(Router.accessToken)"]
                    return ("/assets", params as [String : AnyObject])
                case .article(let type):
                    let params = ["access_token": "\(Router.accessToken)", "content_type": "\(type)"]
                    return ("/entries", params as [String : AnyObject])
                case .top10(let type):
                    let params = ["access_token": "\(Router.accessToken)", "content_type": "\(type)"]
                    return ("/entries", params as [String : AnyObject])
                }
            }()
            let URL = Foundation.URL(string: Router.baseURLString)
            let URLRequest = NSMutableURLRequest(url: URL!.appendingPathComponent(path))
            let encoding = Alamofire.ParameterEncoding.URL //error here line below
            return encoding.encode(URLRequest, parameters: parameters).0
        }
    }
}

在倒数第二行出现此错误。

输入' ParameterEncoding'没有会员'网址'

1 个答案:

答案 0 :(得分:0)

更改此代码..

struct ApiHandler {

enum Router: URLRequestConvertible {
    static let baseURLString = "example.com"
    static let accessToken = "4kjn1kjb3213jkb12k3b12kn321k00"

    case categories(String)
    case assets
    case article(String)
    case top10(String)

    var URLRequest: NSMutableURLRequest {
        let (path, parameters) : (String, [String: AnyObject]?) = {
            switch self {
            case .categories(let type):
                let params = ["access_token": "\(Router.accessToken)", "content_type": "\(type)"]
                return ("/entries", params as [String : AnyObject])
            case .assets:
                let params = ["access_token": "\(Router.accessToken)"]
                return ("/assets", params as [String : AnyObject])
            case .article(let type):
                let params = ["access_token": "\(Router.accessToken)", "content_type": "\(type)"]
                return ("/entries", params as [String : AnyObject])
            case .top10(let type):
                let params = ["access_token": "\(Router.accessToken)", "content_type": "\(type)"]
                return ("/entries", params as [String : AnyObject])
            }
        }()
        let URL = Foundation.URL(string: Router.baseURLString)
        let URLRequest = NSMutableURLRequest(url: URL!.appendingPathComponent(path))
        return try! Alamofire.URLEncoding.default.encode(URLRequest as! URLRequestConvertible, with: parameters) as! NSMutableURLRequest
    }
 }
}