简体   繁体   中英

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 :

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.
  • Then i make SMState impl RawRepresentable Protocol

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.


Update

Why i implement 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

@Hamish You are right, i shouldn't insist using RawRepresentable , after a nice sleep, i finally archive what i want :

//: 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 ~
  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
  3. What i am trying to do is much like this library StatefulViewController , and i want to make it more flexible.

Any suggestion is welcome ~

:D

There is already available a specific state machine you could use instead of inventing the wheels: 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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