combine

Updating a @Published variable based on changes in an observed variable

ⅰ亾dé卋堺 提交于 2020-01-05 06:59:45
问题 I have an AppState that can be observed: class AppState: ObservableObject { private init() {} static let shared = AppState() @Published fileprivate(set) var isLoggedIn = false } A View Model should decide which view to show based on the state ( isLoggedIn ): class HostViewModel: ObservableObject, Identifiable { enum DisplayableContent { case welcome case navigationWrapper } @Published var containedView: DisplayableContent = AppState.shared.isLoggedIn ? .navigationWrapper : .welcome } In the

Can a Swift Property Wrapper reference the owner of the property its wrapping?

人盡茶涼 提交于 2020-01-03 07:30:13
问题 From within a property wrapper in Swift, can you someone refer back to the instance of the class or struck that owns the property being wrapped? Using self doesn't obviously work, nor does super . I tried to pass in self to the property wrapper's init() but that doesn't work either because self on Configuration is not yet defined when @propertywrapper is evaluated. My use case is in a class for managing a large number of settings or configurations. If any property is changed, I just want to

How to detect a value change of a Datepicker using SwiftUI and Combine?

被刻印的时光 ゝ 提交于 2020-01-01 19:25:09
问题 How would you detect a change of value of a Datepicker while using SwiftUI and Combine? I need to invoke a method whenever the datepicker wheel is moved, to update a Text and a Slider. I have looked for specific methods to identify the value change (using UIKit it was possible to associate an action to an event), but apparently I haven't found anything useful in the documentation (I've tried the onTapGesture methods, but that's not what I want, since it forces the user to tap the picker to

Apple Combine framework: How to execute multiple Publishers in parallel and wait for all of them to finish?

痴心易碎 提交于 2020-01-01 17:09:11
问题 I am discovering Combine. I wrote methods that make HTTP requests in a "combine" way, for example: func testRawDataTaskPublisher(for url: URL) -> AnyPublisher<Data, Error> { var request = URLRequest(url: url, cachePolicy: .useProtocolCachePolicy, timeoutInterval: 15) request.httpMethod = "GET" return urlSession.dataTaskPublisher(for: request) .tryMap { return $0.data } .eraseToAnyPublisher() } I would like to call the method multiple times and do a task after all, for example: let myURLs:

What does the dollar sign do in this example?

岁酱吖の 提交于 2019-12-31 05:49:06
问题 This tutorial by Apple about SwiftUI uses a dollar sign to bind data, and I‘m having trouble finding more information about this data binding in SwiftUI. Is this some sort of inout type parameter? That uses the ampersand to pass it on. 回答1: The $ is used in conjunction with property delegates. It's not an operator , but a prefix (thanks @matt!). For more about property delegates, see this Swift Evolution document. e.g. in @State var aState = false , State is a property delegate. This means

Add @Publisher behaviour for computed property

别来无恙 提交于 2019-12-30 12:40:53
问题 I am trying to make a ObservableObject that has properties that wrap a UserDefaults variable. In order to conform to ObservableObject , I need to wrap the properties with @Published . Unfortunately, I cannot apply that to computed properties, as I use for the UserDefaults values. How could I make it work? What do I have to do to achieve @Published behaviour? 回答1: For an existing @Published property Here's one way to do it, you can create a lazy property that returns a publisher derived from

Add @Publisher behaviour for computed property

♀尐吖头ヾ 提交于 2019-12-30 12:40:48
问题 I am trying to make a ObservableObject that has properties that wrap a UserDefaults variable. In order to conform to ObservableObject , I need to wrap the properties with @Published . Unfortunately, I cannot apply that to computed properties, as I use for the UserDefaults values. How could I make it work? What do I have to do to achieve @Published behaviour? 回答1: For an existing @Published property Here's one way to do it, you can create a lazy property that returns a publisher derived from

How to observe a TextField value with SwiftUI and Combine?

坚强是说给别人听的谎言 提交于 2019-12-24 12:28:23
问题 I'm trying to execute an action every time a textField 's value is changed. @Published var value: String = "" var body: some View { $value.sink { (val) in print(val) } return TextField($value) } But I get below error. Cannot convert value of type 'Published' to expected argument type 'Binding' 回答1: This should be a non-fragile way of doing it: class MyData: ObservableObject { var value: String = "" { willSet(newValue) { print(newValue) } } } struct ContentView: View { @ObservedObject var data

Error: Initializer 'init(_:)' requires that 'Binding<String>' conform to 'StringProtocol'

萝らか妹 提交于 2019-12-23 19:22:29
问题 I am getting the above error and couldn't figure out how to solve it. I have an array of objects that contain a boolean value, and need to show a toggle for each of these boolean. Below is the code. class Item: Identifiable { var id: String var label: String var isOn: Bool } class Service: ObservableObject { var didChange = PassthroughSubject<Void, Never>() var items: [Item] { didSet { didChange.send(()) } } } struct MyView: View { @ObservedObject var service: Service var body: some View {

Swift Combine: How to create a single publisher from a list of publishers?

邮差的信 提交于 2019-12-23 07:45:32
问题 Using Apple's new Combine framework I want to make multiple requests from each element in a list. Then I want a single result from a reduction of all the the responses. Basically I want to go from list of publishers to a single publisher that holds a list of responses. I've tried making a list of publishers, but I don't know how to reduce that list into a single publisher. And I've tried making a publisher containing a list but I can't flat map a list of publishers. Please look at the