Skip to content

Instantly share code, notes, and snippets.

struct StateMachine<State, Action> {
private(set) var state: State
private let transition: (State, Action) -> State?
init(startState: State, transition: @escaping (State, Action) -> State?) {
self.state = startState
self.transition = transition
}
mutating func apply(action: Action) -> Bool {
import PlaygroundSupport
import SwiftUI
struct DetailView: View {
@Environment(\.isPresented) var isPresented: Binding<Bool>?
@State var count = 0
var body: some View {
VStack(spacing: 20) {
Text("Count: \(count)")
@nixzhu
nixzhu / UIDevice+ScreenModel.swift
Created May 18, 2015 02:33
Add screenModel to UIDevice, and we use it set UI for different iPhone models
import UIKit
extension UIDevice {
enum ScreenModel {
case Classic
case Bigger
case BiggerPlus
}
@nixzhu
nixzhu / cancelableDelayTask.swift
Last active March 11, 2017 17:10
Cancelable Delay Task
import Foundation
typealias CancelableTask = (cancel: Bool) -> Void
func delay(time: NSTimeInterval, work: dispatch_block_t) -> CancelableTask? {
var finalTask: CancelableTask?
var cancelableTask: CancelableTask = { cancel in
if cancel {