iOS RXSwift如何从.success结果获取价值?

时间:2018-08-20 13:59:01

标签: swift3 ios11 rx-swift associated-types

在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
}

1 个答案:

答案 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
}