[英]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)
}
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()
}
}
}
init?(rawValue:SMState.RawValue)
function above, i can't image how to do this. init?(rawValue:SMState.RawValue)
上面的函数,我无法映像该怎么做。 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
}
enum
state own a separate View
, i do different operation in different state, using the View
that the state carry ~ enum
状态拥有一个单独的View
,我使用状态携带的View
在不同的状态下执行不同的操作〜 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
Any suggestion is welcome ~ 任何建议都欢迎〜
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.