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.
ObservedObject is a property wrapper mainly for Views. Use Published instead..
@Published var blePeripheralDevice: BLEPeripheralDevice!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With