Michael Grant

Michael Grant

SwiftUI class notes 2021-12-02

macOS custom menu command

https://developer.apple.com/tutorials/swiftui/creating-a-macos-app

“The pattern for defining focused values resembles the pattern for definining new Environment values: Use a private key to read and write a custom property on the system-defined FocusedValues structure.”

“definining” lol

I am lost. Time to read the Swift language reference?

private struct SelectedLandmarkKey: FocusedValueKey {
    typealias Value = Binding<Landmark>
}

extension FocusedValues {
    var selectedLandmark: Binding<Landmark>? {
        get { self[SelectedLandmarkKey.self] }
        set { self[SelectedLandmarkKey.self] = newValue }
    }
}

FocusedValues is a struct type defined in the SwiftUI framework, a “collection of state exported by the focused view and its ancestors”.

So, within the app, ¿FocusedValues knows if a given Landmark is selected/has focus?

  • [ ] Find a thorough explanation of the <AngleBrackets> syntax

  • [ ] And ¿what the heck is self[SelectedLandmarkKey.self]?

  • [ ] Also find documentation for SwiftUI .tag(x) modifier

  • SelectedLandmarkKey is a struct that I just defined.

  • So SelectedLandmarkKey.self is… a reference to that struct type?

  • And, in the extension, the first self is a reference to (an instance of?) FocusedValues

  • [ ] But FocusedValues is a struct, not an array or a dictionary, so ¿how does the square brackets syntax work with structs?