简体   繁体   English

具有自定义对象原始值的Swift枚举

[英]Swift enum with custom object raw value

I try to implement a state machine for my ViewController, so i create a enum to express the possible state of my ViewController : 我尝试为ViewController实现状态机,所以我创建了一个枚举来表达ViewController的可能状态:

enum SMState:RawRepresentable{
    case empty,data(UIView),failed(UIView),noData(UIView)
}
  • The 4 state of enum is suitable for my ViewController, and some state associate with a custom view to show when ViewController enter the specify state. 枚举的4状态适合我的ViewController,并且某些状态与自定义视图相关联,以在ViewController进入指定状态时显示。
  • Then i make SMState impl RawRepresentable Protocol 然后我使SMState 隐含RawRepresentable协议

enum SMState:RawRepresentable{
        case empty,data(UIView),failed(UIView),noData(UIView)
        typealias RawValue = UIView

    init?(rawValue: SMState.RawValue) {
        // rawValue is a view but i can't judge what to return
        return what???
    }

    var rawValue: UIView{
        switch self {
        case .data(let v):
            return v
        case .failed(let v):
            return v
        case .noData(let v):
            return v
        case .empty:
            return UIView()
        }
    }
}

How should i implement init?(rawValue:SMState.RawValue) function above, i can't image how to do this. 我应该如何实现init?(rawValue:SMState.RawValue)上面的函数,我无法映像该怎么做。


Update 更新

Why i implement RawRepresentable : 为什么我实现RawRepresentable

I think enum is more suitable for representable different state for ViewController instead of Class or Struct , but enum cannot contains stored property , it can only carry a UIView Object through RawRepresentable , any better idea or magic is welcome :D 我认为enum更适合ViewController而不是Class or Struct可表示不同状态,但是enum不能包含stored property ,它只能通过RawRepresentable携带UIView Object,欢迎任何更好的主意或魔术:D

@Hamish You are right, i shouldn't insist using RawRepresentable , after a nice sleep, i finally archive what i want : @Hamish您说得对,我不应该坚持使用RawRepresentable ,经过一番好觉后,我终于将想要的内容存档了:

//: Playground - noun: a place where people can play

import UIKit

enum SMState{
    case empty,data(UIView),failed(UIView),noData(UIView)
}

extension SMState{
    init() {
        self = .empty
    }
    init(failed view:UIView) {
        self = .failed(view)
    }
    init(data view:UIView) {
        self = .data(view)
    }
    init(noData view:UIView) {
        self = .noData(view)
    }
}

let state = SMState(failed: UIView())


switch state {

case .failed(let v):
    print(v)

default:break

}
  1. This is what i want, each enum state own a separate View , i do different operation in different state, using the View that the state carry ~ 这就是我想要的,每个enum状态拥有一个单独的View ,我使用状态携带的View在不同的状态下执行不同的操作〜
  2. This approach is much like using Class , in my situation, i am not sure that there may be more states, then it is impossible to extend or subclass the SMState because its a enum , i am think about using Class 这种方法很像使用Class ,在我的情况下,我不确定可能会有更多的状态,所以不可能扩展或子类化SMState因为它是一个enum ,我正在考虑使用Class
  3. What i am trying to do is much like this library StatefulViewController , and i want to make it more flexible. 我想做的事情很像这个库StatefulViewController ,我想使其更加灵活。

Any suggestion is welcome ~ 任何建议都欢迎〜

:D :d

There is already available a specific state machine you could use instead of inventing the wheels: GKStateMachine . 您已经可以使用一种特定的状态机来代替发明轮子: GKStateMachine It has all the required method to transition from state to state. 它具有从状态转换到状态的所有必需方法。 We also use it for different view controller states. 我们还将它用于不同的视图控制器状态。 Just take you time to read a bit and find some examples, to have your code nice and clean. 只需花一些时间阅读一下并找到一些示例,以使您的代码简洁明了。 You will also be able to combine the implementation with a corresponding enum of states, which will just define the states without a need of having associated values there. 您还可以将实现与相应的状态枚举结合起来,状态枚举仅定义状态而无需在此处具有关联的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM