由纯 Swift 2 实现的 Flux 集成。受 alt 启发。
要运行示例项目,首先克隆仓库,然后从 Example 目录运行pod install
。
import UIKit
import Swalt
struct CounterActions {
static let Increment = Action("increment")
}
class ClicksStore: Store {
override var initialState: State {
return ["count": 0]
}
required init(_ swalt: Swalt) {
super.init(swalt)
bindAction(CounterActions.Increment) { _payload in
let current = self.state["count"]! as! Int
self.state = ["count": current + 1]
}
}
}
class Flux: Swalt {
static let shared = Flux()
override init() {
super.init()
addStore(ClicksStore)
}
}
class ViewController: UIViewController {
@IBOutlet weak var countLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
Flux.shared.getStore(ClicksStore).listen { state in
let count = state["count"] as! Int
self.countLabel.text = String(count)
}
}
@IBAction func doIt() {
Swalt.instance.dispatch(CounterActions.Increment, payload: nil)
}
}
Swalt 通过 CocoaPods 提供使用。要安装它,只需将以下行添加到您的 Podfile:
pod "Swalt"
Mikkel Malmberg, [email protected]
Swalt 在 MIT 许可协议下提供。有关更多信息,请参阅 LICENSE 文件。