I'm working on my first SwiftUI project and am running into an issue with ObservableObject that I'm at a loss for. The following is a simplified version of my code that reproduces the issue:
import Foundation
protocol ObjectProtocol: ObservableObject {
var value: String { get }
}
class Object: ObjectProtocol {
@Published private(set) var value: String
init(value: String) {
self.value = value
}
}
and my content view:
import SwiftUI
struct ContentView: View {
@ObservedObject var object: Object
var body: some View {
VStack {
Text(object.value)
}
}
}
When I run this, I get the error "Thread 1: EXC_BAD_ACCESS (code=2, address=0x104465d48)" at the "Text(object.value)" line of ContentView. Interestingly, though, the error no longer occurs and it runs as expected when I change ObjectProtocol to:
protocol ObjectProtocol: ObservableObject {
// var value: String { get }
}
Does anyone have an idea as to what is causing this? Is this a bug with SwiftUI/ObservableObject, or am I misunderstanding something?
(running on iOS 13.0, Swift 5, Xcode 11.3.1)