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

It throw fatal error for data in "  self.data.selectedData = i", even I try to calling @State for solve, but nothing change.

Main:
Code Block
struct Main : View {
   
  @EnvironmentObject var data : msgDatas
  @State var show: Bool = false
   
  var body : some View{
     
    List(msgs){i in
       
      cellView(pic: i.pic, name: i.name, msg: i.msg, time: i.time, msgs: i.msgs).onTapGesture {
         
        self.data.selectedData = i
        self.show.toggle()
      }
    }
  }
}

cellView:

Code Block struct cellView : View {
   
  var pic : String
  var name : String
  var msg : String
  var time : String
  var msgs : String
   
  var body : some View{
     
    HStack(spacing: 15){
       
      Image(pic).resizable().frame(width: 50, height: 50).clipShape(Circle())
       
      VStack(alignment:.leading,spacing: 5){
         
        Text(name)
        Text(msg).lineLimit(2)
      }
       
      Spacer()
       
      VStack(spacing: 10){
         
        Text(time)
        if msgs != ""{
           
          Text(msgs).padding(8).background(Color("bg")).foregroundColor(.white).clipShape(Circle())
        }
        else{
           
          Spacer()
        }
      }
       
    }.padding(9)
  }
}


I try to calling @State

That means @State cannot be a solution to solve No ObservableObject.
Please show your code instantiating Main.
I did not get it what u mean?

I did not get it what u mean?

To solve Fatal Error: No ObservableObject ..., you need to call environmentObject(_:) somewhere in your code.
Adding an @State variable has nothing to do with solving No ObservableObject.
here it is,

Code Block class msgDatas : ObservableObject{
   
  @Published var show : Bool = false
  @Published var selectedData : msgType = .init(id: -1, msg: "", time: "", msgs: "", name: "", pic: "")
}


here it is, 

Where? Or what is it?

I first wrote Please show your code instantiating Main.
Your newly shown code does not contain anything about Main.

I then wrote you need to call environmentObject(_:) somewhere in your code.
But I cannot find environmentObject anywhere?
brother in all question u are just asking interesting question, but no any solution, pls I need help. it is clear question.

SceneDelegate:

Code Block
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
  var window: UIWindow?
  func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
    // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
    // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
    // Create the SwiftUI view that provides the window contents.
    let contentView = ContentView()
    let msgdata = msgDatas()
    
    // Use a UIHostingController as window root view controller.
    if let windowScene = scene as? UIWindowScene {
      let window = UIWindow(windowScene: windowScene)
      window.rootViewController = UIHostingController(rootView: contentView.environmentObject(msgdata))
      self.window = window
      window.makeKeyAndVisible()
    }
  }


u are just asking interesting question, but no any solution,

I am asking questions not for my interest, but for finding solutions.
And at last, you have shown a code which could be very helpful to find a solution.
(Sorry, but may not be enough.)

At a glance, I cannot find what's wrong and take some more time to explore.
I show all code, u are not intend to solve problems.



I tried your code and I could not reproduce the error No ObservableObject.

I needed to guess many parts you have not shown, so that may be causing the difference.

Once, again. Please show your code instantiating Main.
(Please do not omit the declaration header, and please include all relevant codes.)

I show all code, u are not intend to solve problems.

Far from all code. I get an error Cannot find 'msgs' in scope with your Main.

If you really want to solve your issue, please show enough info to solve it.
here it is I gave some code about it:

Code Block struct msgType : Identifiable {
   
  var id : Int
  var msg : String
  var time : String
  var msgs : String
  var name : String
  var pic : String
}
// I already made a sample data....
var msgs : [msgType] = [
   
  msgType(id: 0, msg: "New Album Is Going To Be Released!!!!", time: "14:32", msgs: "2", name: "Taylor", pic: "p0") 
]


here it is I gave some code about it:

Sorry, but you tend to omit the first line of codes. What sort of line comes before var id : Int

And again, Please show your code instantiating Main.

In other words, please show all the code including Main().
Code Block struct chatTopview : View {
   
  @EnvironmentObject var data : msgDatas
  @State var show: Bool = false
   
   
  var body : some View{
     
     
    HStack(spacing : 15){
       
      Button(action: {
         
        self.data.show.toggle()
         
      }) {
         
        Image(systemName: "control").font(.title).rotationEffect(.init(degrees: -90))
      }
       
      Spacer()
       
      VStack(spacing: 5){
         
        Image(data.selectedData.pic).resizable().frame(width: 45, height: 45).clipShape(Circle())
         
        Text(data.selectedData.name).fontWeight(.heavy)
         
      }.offset(x: 25)
       
       
      Spacer()
       
      Button(action: {
         
      }) {
         
        Image(systemName: "phone.fill").resizable().frame(width: 20, height: 20)
         
      }.padding(.trailing, 25)
       
      Button(action: {
         
      }) {
         
        Image(systemName: "video.fill").resizable().frame(width: 23, height: 16)
      }
       
      }.foregroundColor(.white)
      .padding()
  }
}

and chatCell:

Code Block struct chatCell : View {
   
  var data : msgdataType
   
  var body : some View{
     
    HStack{
       
      if data.myMsg{
         
        Spacer()
         
        Text(data.msg)
          .padding()
          .background(Color("bg"))
          .clipShape(msgTail(mymsg: data.myMsg))
          .foregroundColor(.white)
      }
      else{
         
        Text(data.msg)
          .padding()
          .background(Color("txtbox"))
          .clipShape(msgTail(mymsg: data.myMsg))
         
        Spacer()
      }
       
    }.padding(data.myMsg ? .leading : .trailing, 55)
    .padding(.vertical,10)
  }
}


Thanks for showing additional codes. But what relation are there between chatTopview or chatCell and already shown codes?

In your actual project, chatTopview is the actual Main? Or something else? I cannot find any lines including Main().

Please understand, we readers do not have any info about what you want to do or how your project is made, Please, please show enough info, if you really want to collaborate with readers to solve your issue.
Code Block struct Home: View {
  @EnvironmentObject var data : msgDatas
  @State var show: Bool = false
 
   
  var body : some View{
     
    ZStack{
       
      Color("bg").edgesIgnoringSafeArea(.top)
       
      NavigationLink(destination: Chat(), isActive: self.$show) {
       
        Text("")
      }
      VStack{
         
        topView()
      }
    }
  }
}
struct topView : View {
   
  var body : some View{
     
    VStack{
       
      HStack(spacing: 15){
         
        Text("Chats").fontWeight(.heavy).font(.system(size: 23))
         
        Spacer()
         
        Button(action: {
           
        }) {
           
          Image(systemName: "magnifyingglass").resizable().frame(width: 20, height: 20)
        }
         
        Button(action: {
           
        }) {
           
          Image("menu").resizable().frame(width: 20, height: 20)
        }
         
      }
      .foregroundColor(Color.white)
      .padding()
       
      GeometryReader{_ in
         
        Main().clipShape(Rounded())
      }
    }
     
  }
}


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