swift - 在swift中以默认情况访问switch case参数
问题描述
我有一个 URLRequestConvertible 枚举(AlamoFire),带有案例
getId(String),
getList(String, String),
getFullList(String, String)
等等。
在里面
public func asURLRequest() throws -> URLRequest {
方法,我需要访问所有情况下通用的第一个参数。我想知道它是否可以在默认情况下访问,或者有没有办法以通用方式提及所有情况。像元组中的变量。
现在我正在访问这样的论点
var headerValue : String {
switch self {
case .getId(let value):
return value
case .getList(let value, _):
return value
case .getFullList(let value, _):
return value
}
}
我期待类似的东西
case let check(let value, _): return value
欢迎任何关于我如何从这里处理它的建议
解决方案
您至少可以将其稍微简化为
var headerValue : String {
switch self {
case .getId(let value),
.getList(let value, _),
.getFullList(let value, _):
return value
}
}
因为所有情况都将相同的变量绑定到相同的类型。
推荐阅读
- spring-cloud-gateway - 在 Spring Cloud 的 GatewayFilter 中注入 bean
- r - 仅当特定值不使用 r 时才绑定表
- reactjs - .NetCore React JS:如何防止 .gltf 文件被路由?
- xml - 如何在 SSIS 中编辑 WSDL (XML)
- javascript - 多选功能中的值显示不正确
- python - 我需要编写一个函数,该函数返回一个数字在另一个列表中出现次数的列表
- javascript - 向节点 js 发送发布请求时,我得到一个空对象
- wordpress - 预加载器不会在移动设备上停止 - wordpress
- python - 如何将列表中的字符串类型字典转换为字典
- python - Django 发送电子邮件错误:ConnectionRefusedError:[Errno 61] 连接被拒绝