Hi folks
I'm playing with the new @AppStorage wrapper because I'm considering using it to store some simple application state data.
I've created a custom class, which conforms to ObservableObject, and RawRepresentable, and it all works fine if I do all my work in a single view.
If I invoke a subview, and pass in the @AppStorage object as an environmentObject, I can read data from it, but updates to the data never propagate back to the underlying storage.
Here is a simplified reproducer, using the new @main/App lifecycle. I would be thrilled if anyone can help me figure out what's wrong:
//
// ContentView.swift
// AppStorageTest
//
// Created by Chris Jones on 25/06/2020.
//
import SwiftUI
import Combine
@main
struct AppStorageTestApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
class Person: RawRepresentable, ObservableObject {
@Published var name: String
@Published var age: Int
// RawRepresentable
var rawValue: String {
get {
let value = "\(name):\(age)"
print("Returning raw value: "+value)
return value
}
}
required init?(rawValue: String) {
if rawValue == "" {
print("Initialised with empty rawValue, setting defaults")
self.name = "Jonny Appleseed"
self.age = 42
return
}
print("Initialising with rawValue: \(rawValue)")
let parts = rawValue.split(separator: ":")
self.name = String(parts[0])
self.age = Int(parts[1])!
}
}
struct ContentView: View {
@AppStorage("person") var person: Person = Person(rawValue: "")!
var body: some View {
// Swap this subview out for the VStack{} from ContentSubview and this all works
ContentSubview().environmentObject(person)
}
}
struct ContentSubview: View {
@EnvironmentObject var person: Person
var body: some View {
VStack {
TextField("Name: ", text: $person.name).padding()
TextField("Age: ", value: $person.age, formatter: NumberFormatter()).padding()
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView().environmentObject(Person(rawValue: "")!)
}
}
Post
Replies
Boosts
Views
Activity
I've just upgraded to Xcode 12 beta 3 and my app, which was building fine on beta 2 now fails to build with the error:
'init(selection:content:)' is unavailable in watchOS
which refers to a call of the form List(selection: $someBinding) { SomeContentHere() }
The docs seem to suggest that this initialiser is supposed to be available for watchOS.
Is anyone else seeing this?
I'm trying to add QuickLook previews to a SwiftUI app which uses Table().
I've added the .quickLookPreview() modifier to my Table(), and added a menu command for invoking it, and if I select that menu item manually, it works fine, but I have two keyboard related issues which are making it difficult to actually ship this functionality:
When using the .quickLookPreview() variant for a set of URLs, keyboard navigation between the quicklook previews only works with left/right arrows, but being invoked by a Table, it would make much more sense for up/down arrows to navigate through the previews
I set a .keyboardShortcut() on the menu command to use Space, since that's the normally-expected shortcut for quicklook, and it doesn't work. If I set it to some random other key (like "a") it does work, but .space doesn't do anything.