在RXSwift中获取与.success结果关联的值的正确语法是什么?
.map{ (result : Result<Double>) -> Double in
//how to get duration from result? this does not work
let duration : Double = result.success.value
return duration
}
答案 0 :(得分:0)
添加通用扩展名:
enum Result<T> {
case success(T)
case failure(Error)
}
extension Result {
var successResult: T? {
guard case let .success(value) = self else { return nil }
return value
}
}
通过以上操作,您可以做到:
result.successResult.map { duration in
// do something with duration
}