Fatal error: No ObservableObject of type TrimmedContext found. A View.environmentObject(_:) for TrimmedContext may be missing as an ancestor of this view.

I get "Fatal error: No ObservableObject of type TrimmedContext found. A View.environmentObject(_:) for TrimmedContext may be missing as an ancestor of this view." I am running a dumping wrapper on the view where the object is used, and it appears in the property list, only when the property wrapper is not declared.

However, when using @Trimmed propertyWrapper on the view, prior to any logs of any kind the crash occurs as if the ContentView is being loaded before all else. I've tried putting the view on WindowGroup, the view itself and a variety of other places without results.

This code works on others machine, just not mine. I am running a new MacPro with M2Max. 12-Core CPU, 30-Core GPU, 32GB Unified Memory, 1TB SSD Storage should this matter. Since the code builds on others machine, I wonder if it might be related to the new equipment and possibly a bug on Apple's side:

Below is a simple example of the code:

`

import SwiftUI

struct ContentView: View {

    

    @Trimmed var url = "rebeloper.com/mentoring "

    

    var body: some View {

        VStack {

            Image(systemName: "globe")

                .imageScale(.large)

                .foregroundColor(.accentColor)

            Text("Hello, world!")

            Button("") {

                

            }

        }

        .padding()

    }

}

struct ContentView_Previews: PreviewProvider {

    static var previews: some View {

        ContentView()

    }

}

@propertyWrapper

struct Trimmed: DynamicProperty {

    

    @EnvironmentObject var context: TrimmedContext

    

    init(wrappedValue: String) {

        context.value = wrappedValue

    }

    

    var wrappedValue: String {

        get {

            context.value.trimmingCharacters(in: .whitespacesAndNewlines)

        }

        set {

            context.value = newValue

        }

    }

}

class TrimmedContext: ObservableObject {

    @Published var value: String = ""

}

struct TrimmedView<C: View>: View {

    

    @StateObject private var context = TrimmedContext()

    

    var content: () -> C

    

    var body: some View {

        content()

            .environmentObject(context)

    }

}

extension View {

    func trimmedView() -> some View {

        TrimmedView {

            self

        }

    }

} `

` import SwiftUI

@main

struct CustomPropertyWrappersForBeginnersApp: App {

    @StateObject var trimmed: TrimmedContext = TrimmedContext()

    var body: some Scene {

        WindowGroup {

            ContentView()

                .environmentObject(trimmed)

        }

    }

}`

Answered by MobileTen in 743654022

You cannot use @EnvironmentObject var context: TrimmedContext outside of a View. Remove @EnvironmentObject from the property wrapper. Change  @StateObject in TrimmedView to @EnvironmentObject

You cannot use @EnvironmentObject var context: TrimmedContext outside of a View. Remove @EnvironmentObject from the property wrapper. Change  @StateObject in TrimmedView to @EnvironmentObject 

Accepted Answer

You cannot use @EnvironmentObject var context: TrimmedContext outside of a View. Remove @EnvironmentObject from the property wrapper. Change  @StateObject in TrimmedView to @EnvironmentObject

Fatal error: No ObservableObject of type TrimmedContext found. A View.environmentObject(_:) for TrimmedContext may be missing as an ancestor of this view.
 
 
Q