1

Having problems with appending to an array in SwiftUI.

I'm on Xcode 11 beta 7 and using the updated ObservableObject, EnvironmentObject and Published syntax on a watchOS app. WKHostingController expects concrete types so passing ContentView().environmentObject(subject) is not possible.

This line crashes the app:

self.subjectData.store.append(Subject(name: self.addedSubject, isFavorite: false)) 

Any ideas on what is wrong?

struct Subject: Codable, Identifiable {
    var id: UUID = UUID()
    var name: String
    var isFavorite: Bool
}

class SubjectDataEnv: ObservableObject {
    @Published var store = [
        Subject(name: "Physics", isFavorite: true),
        Subject(name: "Science", isFavorite: false)
    ]
    @Published var selectedSubject = Subject(name: "Subject 1", isFavorite: false)
}

class HostingController: WKHostingController<ContentView> {
    override var body: ContentView {
        return ContentView()
    }
}

struct ContentView: View {
    @State var subjectData = SubjectDataEnv()

    var body: some View {
        SubjectView().environmentObject(subjectData)
    }
}

struct SubjectView: View {
    @EnvironmentObject var subjectData: SubjectDataEnv
    var body: some View {
        List {
            ForEach(subjectData.store) { subject in
                NavigationLink(destination: DurationView()
                    .environmentObject(self.subjectData)
                )
            }
            NavigationLink(destination: AddSubjectView()
                .environmentObject(self.subjectData)
            ) {
                Text("+")
            }
        }
    }
}

struct AddSubjectView: View {  
    @EnvironmentObject var subjectData: SubjectDataEnv
    @State var addedSubject: String = "subject"
    var body: some View {
        VStack(alignment: .leading, spacing: 0) {
            TextField("Add your subject", text: $addedSubject)
            Button(action: {
                self.subjectData.store.append(Subject(name: self.addedSubject, isFavorite: false))   // crashes the app
            }) {
                Text("Done")
            }
        }
    }
}
Sid
  • 963
  • 1
  • 12
  • 17
  • Could you also please post the error message you are facing? – backslash-f Aug 30 '19 at 17:24
  • I'm not very good with understanding the debugger but this is what I see. SwiftUI`SwiftUI.UIKitNavigationBridge.(push in _F345616596EA75D1F4200D7666E5E588)(_: SwiftUI.NavigationDestinationContent, onto: __C.UINavigationController, animated: Swift.Bool) -> (): – Sid Aug 31 '19 at 00:27

1 Answers1

2

Two approaches are possible:

  1. Using AnyView
class HostingController: WKHostingController<AnyView> {
    override var body: AnyView {
        return AnyView(AddSubjectView().environmentObject(SubjectDataEnv()))
    }
}

result

Here's the output (I attached a print in the button):

print(self.subjectData.store)
Subject(id: 2F3745EE-752B-4C8C-8A4F-8C21E73B315C, name: "Physics", isFavorite: true)
Subject(id: 5A9CF711-F6BE-4854-8F58-35FD004B2419, name: "Science", isFavorite: false)
Subject(id: E2F6A25E-3BC0-4D17-9AEF-07286533D475, name: "", isFavorite: false)
Subject(id: 62FA7D12-A3D5-4381-8232-F48268B433F1, name: "☺️", isFavorite: false)

Notice the new and ☺️ subjects there.

  1. Injecting the @EnvironmenObject in a "parent view"
class HostingController: WKHostingController<ContentView> {
    override var body: ContentView {
        return ContentView()
    }
}
struct ContentView: View {
    var body: some View {
        return AddSubjectView().environmentObject(SubjectDataEnv())
    }
}

The latter will produce the same results.

backslash-f
  • 7,923
  • 7
  • 52
  • 80
  • Given WKHostingController expects a Concrete type I am not able to do the following ContentView().environmentObject(UserData()) (re:https://stackoverflow.com/questions/57559200/how-to-inject-environmentobject-in-watchos6) so I was trying the work around in that answer. @Env works with an iPhone app so I have no issues there. – Sid Aug 31 '19 at 00:45
  • Got it. I updated my answer, please take another look. – backslash-f Aug 31 '19 at 07:44
  • I've added more code. The shortened version works for me too. Hmmm. It's not related to the WK/Env elements. I was injecting the @EnvironmentObject via the 'parent view' method - which is now updated above. Thanks. – Sid Aug 31 '19 at 18:06