0

In my open source SwiftUI project a certain piece of code stopped working as of Xcode 11 b6 with the following error: Type of expression is ambiguous without more context. at the line ModalView(currentModal: modal) Everything worked without a hitch before that.

struct ContentView: View {

    @EnvironmentObject var modalManager: ModalManager

    var body: some View {
        return ZStack {
            ForEach($modalManager.modals) { modal in
                ModalView(currentModal: modal) ## Error here
                    .environmentObject(self.modalManager)
            }
        }.onAppear(perform: {self.modalManager.fetchContent()})
    }
}

Modal conforms to Identifiable as shown below so that's not the issue.

struct Modal: Identifiable {
    let id = UUID()
    var content: AnyView
    var position: ModalState  = .base

    var isFullscreenEnabled: Bool = false
    var dragOffset: CGSize = .zero
}

Changing the ForEach loop to ForEach($modalManager.modals, id: \.self) { modal in ....} returns a more confusing error message: Generic parameter 'B' could not be inferred at the line setting the environment object.

Any help would be greatly appreciated.

cyril
  • 3,020
  • 6
  • 36
  • 61
  • What is `ModalView` and what is `ModalManager`? – Mojtaba Hosseini Aug 22 '19 at 05:34
  • Why are you injecting the modalManager into the ModalView? It's an environment object and you already have it in the ContentView. So it will be available to the whole view hierarchy without the need to manually pass it to every subviews (this is exactly the difference between an EnvironmentObject and an ObservedObject). – superpuccio Aug 22 '19 at 08:34
  • @superpuccio he's passing a binding in but the conditional was removed. I agree that he has to use `.indexed()` or it has to be rewritten. – Fabian Aug 22 '19 at 08:53
  • @superpuccio it's a design choice. The open-source project is supposed to act like a package manager. The user would set the content of the modals using the EnvironmentObject and not have to worry about anything else. – cyril Aug 22 '19 at 15:45
  • @Fabian so how would I go about fixing it? – cyril Aug 22 '19 at 15:45
  • 1
    See the answer I posted to the question above. And apply the fix from the comments. That should do it. – Fabian Aug 22 '19 at 15:47
  • Ah just saw that, thanks! Are there any downsides to doing this? And how would I go about fixing the code without IndexedCollection? – cyril Aug 22 '19 at 15:49
  • 1
    You want a binding in there. To get it from the `modalManager` you need an index to use. To get that index you can use `.firstIndex` or some such where you use the passed-in element and compare by id to get it, and pass that to `$modalManager.modals[]`. Or you pass the element in and have the view inside do the same by accessing the `ObservableObject`. :-) Or... You let `IndexedCollection` give you the index to avoid all that. – Fabian Aug 22 '19 at 15:55
  • @Fabian I get the error: Type '_' has no member '1' which makes no sense. Would you mind trying out your code in the project I mentioned above? – cyril Aug 22 '19 at 20:47
  • I know what you mean. I edited the other post to show how it might work. `\.1.id` does indeed not work. Always those release notes :P – Fabian Aug 22 '19 at 21:19

0 Answers0