Home > Enterprise >  Thread 1: Fatal error: No ObservableObject of type Devices found. A View.environmentObject(_:) for D
Thread 1: Fatal error: No ObservableObject of type Devices found. A View.environmentObject(_:) for D

Time:11-17

Hi I am trying to make an EnvironmentObject, but something went wrong and I don't understand what's matter with code. I tried to find the solution, but they didn't help.

import SwiftUI

struct Device1View: View {
    var iPhones: [iPhone] = iPhone.allIphones
    @EnvironmentObject var device: Devices
    var body: some View {
        Form{
            ForEach(iPhones, id: \.self){phone in
                Button(phone.model){
                    _ = device.selectedDevice1
                }
            }
        }
    }
}
import SwiftUI

class Devices: ObservableObject{
    @Published  var selectedDevice1 = iPhone.sampleiPhone
    @Published  var selectedDevice2 = iPhone.sampleiPhone
}

struct CompareView: View {
    @Environment(\.colorScheme) var colorScheme
    
    @StateObject var device = Devices()
    
    var iPhones: [iPhone] = iPhone.allIphones
    
    let columns = [
        GridItem(.adaptive(minimum: 200, maximum: 250)),
        GridItem(.adaptive(minimum: 200, maximum: 250))
    ]
    
    var body: some View {
        ZStack(alignment: .top){
            ScrollView{...}
            .padding(.top, 55)
            
            HStack(alignment: .center, spacing: 10){
                NavigationLink(destination: Device1View()) {
                    Text(device.selectedDevice1.model)
                }
                
                Divider()
            }
            
            .frame(height: 50)
            .background(CustomColor.myColor)
            .cornerRadius(25)
            .shadow(radius: 8, x: 5, y: 5)
        }
    }
}
import SwiftUI

struct CustomColor{
    static let myColor = Color("mycolour")
}

struct ContentView: View {
    var body: some View {
        NavigationView{
            CompareView().environmentObject(Devices())
        }
    }
}

As I said i tried to find a solution using this FatalError, I have an experience with this problem, but the project was not so big.

CodePudding user response:

Did you declare Devices in your ContentView, Joakim stated?

struct ContentView: View {
@StateObject var device = Devices()

var body: some View {
    NavigationView {
        CompareView().environmentObject(device)
    }
  }
}

Then change the declaration in CompareView to

@EnvironmentObject var device: Devices

CodePudding user response:

struct iPhone: Codable, Hashable{
let model: String
let platform: Platform
let body: Body
let display: Display
let rearCamera: RearCamera
let frontCamera: FrontCamera
let storage: Array<String>
let battery: Battery
let authentication: Authentication
let processor: Processor
let memory: Memory



static let allIphones: [iPhone] = Bundle.main.decode(file: "iphones.json")
static let sampleiPhone: iPhone = allIphones[0]

}

struct Platform: Codable, Hashable{
let year: String
let firstIOS: String
let latestIOS: String

}

struct Body: Codable, Hashable{
let dimensions: Array<String>
let weight: Array<String>
let sim: Array<String>

}

  • Related