Alamofire 4.0 路由器表达式类型不明确,没有更多上下文
Posted
技术标签:
【中文标题】Alamofire 4.0 路由器表达式类型不明确,没有更多上下文【英文标题】:Alamofire 4.0 Router Type of expression is ambiguous without more context 【发布时间】:2016-09-22 19:37:54 【问题描述】:我正在转换为Swift 3.0
,并且有一个用于Alamofire
的自定义Router
类。我的代码如下:
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
这给了我一个错误,在 Alamofire.URLEncoding()
处声明 Type of expression is ambiguious without more context
。这是什么意思?
在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
【问题讨论】:
【参考方案1】:这几乎是 How to migrate Alamofire router class to Swift 3? 的副本
检查一下,您将了解如何使用新的ParameterEncoding
,尤其是URLRequestConvertible
现在是用func asURLRequest() throws -> URLRequest
实现的,而不是var
。
【讨论】:
感谢实现func
而不是 var
确实有帮助。以上是关于Alamofire 4.0 路由器表达式类型不明确,没有更多上下文的主要内容,如果未能解决你的问题,请参考以下文章
Alamofire 4.0 上的“类型‘任何’没有下标成员”错误
尝试通过 Alamofire 4.0 上传图像时,类型“ParameterEncoding”没有成员“URL”
使用 Swift 3 的 Alamofire 4.0 出现“没有成员”错误
无法在 Alamofire 4.0 中使用类型为“(String,withName:String)”的参数列表调用“附加”