You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When working with Core Data, there are times we have optional NSManagedObject to pass around. These objects conform to ObservableObject, and in SwiftUI we can't @ObservedObject on optional ObservableObject
One way we can workaround this is to introduce a wrapper type that checks and internally observes the ObservableObject
structObserveView<T:ObservableObject, Content:View>:View{varobject:T?@ViewBuildervarcontent:(T)->ContentstructInnerView:View{@ObservedObjectvarobject:Tvarcontent:(T)->Contentvarbody:someView{content(object)}}varbody:someView{
if let object {InnerView(object: object, content: content)}}}@ViewBuilderfunc observe<T:ObservableObject, Content:View>(_ object:T?,@ViewBuilder content:@escaping(T)->Content)->someView{ObserveView(object: object, content: content)}
So we can use like
varbook:Book?observe(book){ book inText(book.name)}
The text was updated successfully, but these errors were encountered:
When working with Core Data, there are times we have optional
NSManagedObject
to pass around. These objects conform toObservableObject
, and in SwiftUI we can't@ObservedObject
on optionalObservableObject
One way we can workaround this is to introduce a wrapper type that checks and internally observes the
ObservableObject
So we can use like
The text was updated successfully, but these errors were encountered: