Post

Replies

Boosts

Views

Activity

Cannot convert Binding<Type> to Binding<Protocol>
In Xcode 12: I have a protocol: protocol Parameter { } I then have several structs conforming to this protocol: struct Temperature: Parameter { } ... I have a ParameterView that takes a parameter and builds out a view: struct ParameterView: View { @Binding var parameter: Parameter ... } Then I have a ParametersView (multiple parameters together now) that uses multiple parameters: struct ParametersView: View { @Binding var temperature: Temperature  var body: some View {   VStack(alignment: .leading) { ParameterView(parameter: $temperature) ... } } } The compiler does not like this: Cannot convert value of type 'Binding&lt;Temperature&gt;' to expected argument type 'Binding&lt;Parameter&gt;' I must be violating something, but being a little rusty, I cannot determine if this is expected behavior or a compiler problem or something I completely messed up. Any assistance would be appreciated
2
0
6.2k
Jun ’20
NSManagedObject properties becoming nil when passed to view from PreviewProvider
I have a view that will display available genres. Genre is a NSManagedObject containing the title of the genre. I am trying to create a Samples class that provides sample data to be used in the PreviewProvider. However, the Genre returned from the Samples class has its genre string set to nil. For the life of me...I cannot figure out why this is happening: class Samples {     static var sampleGenre: Genre {         let context = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)         let genre = Genre(context: context)         genre.genre = "Horror"         return genre     } } Then in the PreviewProvider: struct GenreRow_Previews: PreviewProvider {     static var previews: some View {         GenreRow(genre: Samples.sampleGenre)     } } The GenreRow is simple: struct GenreRow: View {    var genre: Genre     var body: some View {         HStack {             Text(genre.genre ?? "Unknown genre")         }         .padding()     } } The text is set to "Unknown genre" even though one has previously been set. Any guidance would be appreciate. How are people previewing views with NSManagedObjects?
0
0
399
Aug ’20
Recommended method of passing NSManagedObjectContext to @ObservedObject/@StateObject
I have a view that is pulling the context from the environment using the latest SwiftUI CoreData template (i.e. there is a preview/in-memory context vs the persistent context). I also have an @ObservableObject class that is fetching objects based on the predicates passed in (think dynamic filtering). A random element from these fetched results are then displayed back in the view (i.e. I do not need this ObservableObject class to be a view itself). However, there is an interesting "issue" where I cannot instantiate my @ObservedObject because the property initializers are run before "self" is available and I need to pass it the NSManagedObjectContext. The only way I can think to get around this is to create the ObservableObject class outside of the view and pass it in the view's initializer. However, this isn't completely desirable as I would prefer this data be completely private to the view as other views do not need to know about its existence. I also need it to be an ObserableObject so that the filters can change and it be reflected back in the view observing it. Am I using the wrong tool or thinking about this wrong? class Filter: ObservableObject { @Published var someObjects: [Objects] = [] /* Need to instantiate with the context so objects can be fetched */ private let context: NSManagedObjectContext } struct ContentView: View {     @Environment(\.managedObjectContext) var context &#9; /* Cannot initialize here as context isn't available */     @ObservedObject var filter = Filter(context: context) }
4
1
2.5k
Aug ’20