Skip to content

Instantly share code, notes, and snippets.

@drewmccormack
Created February 25, 2020 18:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drewmccormack/09e47ff44572c23015d29dbce8e0fb96 to your computer and use it in GitHub Desktop.
Save drewmccormack/09e47ff44572c23015d29dbce8e0fb96 to your computer and use it in GitHub Desktop.
Simple example of MMVM with SwiftUI.
import Foundation
import SwiftUI
import Combine
let dateFormatter: DateFormatter = {
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
return dateFormatter
}()
class MainController: ObservableObject {
static let shared = MainController()
@Published var contentViewModel: ContentView.Model = .init()
}
struct ContentView: View {
class Model: ObservableObject {
@Published var dateString: String = dateFormatter.string(from: Date())
}
@ObservedObject var model: Model
var body: some View {
VStack {
Text(model.dateString)
Button(action: {
self.model.dateString = dateFormatter.string(from: Date())
}, label: { Text("Set Date") })
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment