Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generic Struct 'ObservedObject' Requires That Conform To 'ObservableObject

Tags:

swiftui

I am having trouble with understanding why the code below will not compile. I'm getting an error stating that I must conform to ObservableObject and I cannot see why I wouldn't be.

I've simplified to show that I am seeing. I have two classes. The second observes the first and then the view observes the second.

First Class

import Foundation
import SwiftUI
import CoreBluetooth

class BLEPeripheralDevice: NSObject, ObservableObject {
    @Published var bodySesnorLocation: String = ""
}

Second Class

import Foundation
import SwiftUI
import CoreBluetooth

class BLEManager: NSObject, ObservableObject {
    @ObservedObject var blePeripheralDevice: BLEPeripheralDevice!
    
    @Published var blePeripheralName: String = ""
}

View

import SwiftUI

struct BluetoothDeviceView: View {
    @ObservedObject var bleManager = BLEManager()

var body: some View {
        VStack (spacing: 10) {
            Text("Bluetooth Devices")
}
}

When I compile this code I am getting an error in the second class on the following line.

@ObservedObject var blePeripheralDevice: BLEPeripheralDevice!

Generic struct 'ObservedObject' requires that 'BLEPeripheralDevice?' conform to 'ObservableObject'

I don't understand why this would be. Any help is appreciated.

like image 781
jonthornham Avatar asked May 04 '21 20:05

jonthornham


1 Answers

ObservedObject is a property wrapper mainly for Views. Use Published instead..

@Published var blePeripheralDevice: BLEPeripheralDevice!
like image 98
davidev Avatar answered Oct 14 '22 05:10

davidev