Alamofire 4.0 Router Type of expression is ambiguous without more context
Alamofire 4.0 Router Type of expression is ambiguous without more context
我正在转换为 Swift 3.0
,并为 Alamofire
定制了 Router
class。我的代码如下:
enum Router: URLRequestConvertible {
case get(query: String, params: [String: AnyObject]?)
case post(query: String, params: [String: AnyObject]?)
case put(query: String, params: [String: AnyObject]?)
case delete(query: String, params: [String: AnyObject]?)
var urlRequest: NSMutableURLRequest {
// Default to GET
var httpMethod: HTTPMethod = .get
let (path, parameters): (String, [String: AnyObject]?) = {
switch self {
case .get(let query, let params):
// Set the request call
httpMethod = .get
// Return the query
return (query, params)
case .post(let query, let params):
// Set the request call
httpMethod = .post
// Return the query
return (query, params)
case .put(let query, let params):
// Set the request call
httpMethod = .put
// Return the query
return (query, params)
case .delete(let query, let params):
// Set the request call
httpMethod = .delete
// Return the query
return (query, params)
}
}()
// Create the URL Request
let urlRequest: NSMutableURLRequest
if let url = URL(string: Globals.BASE_URL + path) {
urlRequest = NSMutableURLRequest(url: url)
} else {
urlRequest = NSMutableURLRequest()
}
// set header fields
if let key = UserDefaults.standard.string(forKey: Globals.NS_KEY_SESSION) {
urlRequest.setValue(key, forHTTPHeaderField: "X-XX-API")
}
// Add user agent
if let userAgent = UserDefaults.standard.string(forKey: Globals.NS_KEY_USER_AGENT) {
urlRequest.setValue(userAgent, forHTTPHeaderField: "User-Agent")
}
// Set the HTTP method
urlRequest.httpMethod = httpMethod.rawValue
// Set timeout interval to 20 seconds
urlRequest.timeoutInterval = 20
return Alamofire.URLEncoding().encode(urlRequest as! URLRequestConvertible, with: parameters)
}
func asURLRequest() throws -> URLRequest {
}
}
这给我一个错误,说明 Type of expression is ambiguious without more context
在 Alamofire.URLEncoding()
。这是什么意思?
在 https://github.com/Alamofire/Alamofire/blob/master/Documentation/Alamofire%204.0%20Migration%20Guide.md#parameter-encoding-protocol 的 Alamofire
文档中,他们有代码
public protocol ParameterEncoding {
func encode(_ urlRequest: URLRequestConvertible, with parameters: Parameters?) throws -> URLRequest
}
这几乎是 的重复
检查一下,您将了解如何使用新的 ParameterEncoding
,尤其是 URLRequestConvertible
现在是使用 func asURLRequest() throws -> URLRequest
而不是 var
实现的。
我正在转换为 Swift 3.0
,并为 Alamofire
定制了 Router
class。我的代码如下:
enum Router: URLRequestConvertible {
case get(query: String, params: [String: AnyObject]?)
case post(query: String, params: [String: AnyObject]?)
case put(query: String, params: [String: AnyObject]?)
case delete(query: String, params: [String: AnyObject]?)
var urlRequest: NSMutableURLRequest {
// Default to GET
var httpMethod: HTTPMethod = .get
let (path, parameters): (String, [String: AnyObject]?) = {
switch self {
case .get(let query, let params):
// Set the request call
httpMethod = .get
// Return the query
return (query, params)
case .post(let query, let params):
// Set the request call
httpMethod = .post
// Return the query
return (query, params)
case .put(let query, let params):
// Set the request call
httpMethod = .put
// Return the query
return (query, params)
case .delete(let query, let params):
// Set the request call
httpMethod = .delete
// Return the query
return (query, params)
}
}()
// Create the URL Request
let urlRequest: NSMutableURLRequest
if let url = URL(string: Globals.BASE_URL + path) {
urlRequest = NSMutableURLRequest(url: url)
} else {
urlRequest = NSMutableURLRequest()
}
// set header fields
if let key = UserDefaults.standard.string(forKey: Globals.NS_KEY_SESSION) {
urlRequest.setValue(key, forHTTPHeaderField: "X-XX-API")
}
// Add user agent
if let userAgent = UserDefaults.standard.string(forKey: Globals.NS_KEY_USER_AGENT) {
urlRequest.setValue(userAgent, forHTTPHeaderField: "User-Agent")
}
// Set the HTTP method
urlRequest.httpMethod = httpMethod.rawValue
// Set timeout interval to 20 seconds
urlRequest.timeoutInterval = 20
return Alamofire.URLEncoding().encode(urlRequest as! URLRequestConvertible, with: parameters)
}
func asURLRequest() throws -> URLRequest {
}
}
这给我一个错误,说明 Type of expression is ambiguious without more context
在 Alamofire.URLEncoding()
。这是什么意思?
在 https://github.com/Alamofire/Alamofire/blob/master/Documentation/Alamofire%204.0%20Migration%20Guide.md#parameter-encoding-protocol 的 Alamofire
文档中,他们有代码
public protocol ParameterEncoding {
func encode(_ urlRequest: URLRequestConvertible, with parameters: Parameters?) throws -> URLRequest
}
这几乎是 ParameterEncoding
,尤其是 URLRequestConvertible
现在是使用 func asURLRequest() throws -> URLRequest
而不是 var
实现的。