使用参数

时间:2017-07-04 12:16:59

标签: ios swift enums

我有一个用于跟踪网络请求的枚举,它看起来像这样:

enum LoadingStateMachine<T: Equatable>: Equatable {

    /// The machine is awaiting instructions
    case initial

    /// The content is loading
    case loading

    /// the content has loaded
    case loaded(items: [T])

    /// There was an error loading the content
    case error(error: NSError)
}


func ==<T: Equatable>(lhs: LoadingStateMachine<T>, rhs: LoadingStateMachine<T>) -> Bool {

    switch (lhs, rhs) {
    case (.initial, .initial), (.loading, .loading):
        return true

    case (let .error(error1), let .error(error2)):
        return error1 == error2

    case (let .loaded(items1), let .loaded(items2)):
        return items1 == items2

    default:
        return false
    }

}

如果我要将枚举的两个实例都设置为.loaded,我会执行以下操作:

let loadingState = LoadingStateMachine<Int>.loaded(items: [])
if case .loaded(let items) = loadingState {

}

是否可以内联执行此操作? e.g。

let check = case .loaded(let items) = loadingState

0 个答案:

没有答案