【发布时间】:2020-07-26 12:33:43
【问题描述】:
我正在使用 Alamofire 5 为 iOS 13.4(Swift 5、Xcode 11)编写一个具有网络功能的应用程序。我创建了我的自定义类型 typealias KeyedParameters = [ParameterKeys: Any] 以便能够以“快速”的方式使用我的 API 参数键(即.login 而不是KeyedParameters.login.rawValue)。
问题是当我尝试将此类型转换回默认 Alamofire 的 Parameters 时,我收到以下错误:Cannot convert return expression of type 'Dictionary<ParameterKeys, Any>' to return type 'Parameters' (aka 'Dictionary<String, Any>')。
选角:
extension KeyedParameters {
var parameters: Parameters {
Dictionary(uniqueKeysWithValues: map { ($0.key.rawValue, $0.value) })
}
}
参数键:
enum ParameterKeys: String {
// MARK: - Auth and User
case id, login, password, email, name
case createdAt = "created_at"
...
}
错误的样子:
【问题讨论】:
-
我也尝试过字符串插值 ("($0.key.rawValue)")、显式构造函数调用 (String($0.key.rawValue)) 和强制类型转换 ($0.key.rawValue as !String) — 也没有用。
标签: ios swift xcode alamofire swift5