Follow along at https://www.hackingwithswift.com/100/swiftui/36.
This day covers Part One of Project 7: iExpense
in the 100 Days of SwiftUI Challenge.
It focuses on several specific topics:
- iExpense: Introduction
- Why @State only works with structs
- Sharing SwiftUI state with @ObservedObject
- Showing and hiding views
- Deleting items using onDelete()
- Storing user settings with UserDefaults
- Archiving Swift objects with Codable
It's an expense tracker. Reign in your spending... buy Bitcoin... financial responsibility, people 😀.
Super short version: Structs allow us to leverage value semantics for most of our code -- and @State is meant as helper for updating to changes within to that paradigm.
https://twitter.com/cypher_poet/status/1186121232800014338
SwiftUI's modal presentation is flow is really starting to grow on me. As straightforward as showing a modal is, thought (using the .sheet
modifier), dismissal is a bit less so. Fortunately, the @Environment
has our back -- allowing us to trigger dismissal from within a modal view like so:
struct MyView: View {
@Environment(\.presentationMode) var presentationMode
var body: some View {
Button("Dismiss") {
self.presentationMode.wrappedValue.dismiss()
}
}
}
Step 1: The view hierarchy:
List {
ForEach {
}
.onDelete(perform: removeRows)
}
.navigationBarItems(leading: EditButton())
Step 2: The callback:
func removeRows(at offsets: IndexSet) {
numbers.remove(atOffsets: offsets)
}