combine

SwiftUI How to instantiate PreviewProvider when View requires @Binding in initializer

一曲冷凌霜 提交于 2019-12-02 03:49:17
问题 With SwiftUI (Xcode 11.1), I've got some Views set up with 2-way bindings (using @Binding ). Two-way updating works great. However, how can I instantiate the view from the PreviewProvider? For example: struct AddProjectView: View { @Binding public var showModal: Bool var body: some View { return VStack { Text("Add Project View") Button("Dismiss") { self.showModal = false } } } } I can't do this, because "true" is not a Binding: struct AddProjectView_Previews: PreviewProvider { static var

How to change thread using combine Publisher?

∥☆過路亽.° 提交于 2019-12-02 02:00:52
问题 I am using Combine and SwiftUI to do some async stuff, the point is that I don't know how to receive the response from the asynchronous operation in the main thread. The apple doc says that it can be used the RunLoop.main, but currently in Swift 5.0 it isn't a Scheduler. So any ideas about this? I have tried to use as per apple doc, but no luck. anyPublisher .receiveOn(on: RunLoop.main) 回答1: Combine - at the time of writing - is not fully integrated in Foundation . According to Xcode 11 Beta

Index out of range when binding a Slider value to a nested array in EnvironmentObject

醉酒当歌 提交于 2019-12-01 14:20:23
Description: I have a model that has the following hierarchy: Recipe ...steps (an array) ... currentStep ...... parameters (an array) .........minimum .........maximum .........default ......... current The model works well. I can add steps, parameters, and set the current step to an @EnvironmentObject called recipe . I've created a sample project here with I two lists of steps and parameters, along with three buttons to add a single step among three hard-coded ones, each containing an array of 0, 1, or 3 parameters. The top list is the step rows, each being a button to populate the bottom

Index out of range when binding a Slider value to a nested array in EnvironmentObject

余生长醉 提交于 2019-12-01 12:09:08
问题 Description: I have a model that has the following hierarchy: Recipe ...steps (an array) ... currentStep ...... parameters (an array) .........minimum .........maximum .........default ......... current The model works well. I can add steps, parameters, and set the current step to an @EnvironmentObject called recipe . I've created a sample project here with I two lists of steps and parameters, along with three buttons to add a single step among three hard-coded ones, each containing an array

SwiftUI @Binding doesn't refresh View

微笑、不失礼 提交于 2019-12-01 02:13:51
I have a simple master/detail interface where the detail view modifies an item in an array. Using the below, the model is updated properly, but SwiftUI doesn't refresh the View to reflect the change. Model: struct ProduceItem: Identifiable { let id = UUID() let name: String var inventory: Int } final class ItemStore: BindableObject { var willChange = PassthroughSubject<Void, Never>() var items: [ProduceItem] { willSet { willChange.send() } } init(_ items: [ProduceItem]) { self.items = items } } Master view that displays a list of ProduceItems (an ItemStore is inserted into the environment in

How to tell SwiftUI views to bind to nested ObservableObjects

一曲冷凌霜 提交于 2019-12-01 02:12:30
问题 I have a SwiftUI view that takes in an EnvironmentObject called appModel . It then reads the value appModel.submodel.count in its body method. I expect this to bind my view to the property count on submodel so that it re-renders when the property updates, but this does not seem to happen. Is this a bug? And if not, what is the idiomatic way to have views bind to nested properties of environment objects in SwiftUI? Specifically, my model looks like this... class Submodel: ObservableObject {