Swift Macro for Elegant ViewModel & Action Management
dependencies: [
.package(url: "https://github.com/wayne90040/Moon")
]Automatically generates computed properties for extracting enum associated values.
@MoAction
enum Action {
case test1
case test2(String)
case test3(gg: String)
case test4(String, Int)
case test5(g1: String, g2: Int)
case test6(String, g2: Int)
}Generated code:
var test1: Void? {
if case .test1 = self { return () }
return nil
}
var test2: String? {
if case .test2(let p1) = self { return p1 }
return nil
}
var test5: (g1: String, g2: Int)? {
if case .test5(let g1, let g2) = self { return (g1, g2) }
return nil
}Automatically injects standard ViewModel members (action and cancellables).
-
Finds the first
enummarked with@MoAction -
Generates:
let action = PassthroughSubject<Action, Never>()var cancellables = Set<AnyCancellable>()
@MoViewModel
class MyViewModel {
@MoAction
enum Action {
case loadData
case showError(String)
}
}Generated code:
let action = PassthroughSubject<Action, Never>()
var cancellables = Set<AnyCancellable>()