The explanation available at:
State | Apple Developer Documentation
Shows an example where a play/pause button is defined in the SwiftUI framework. The communication between the views is accomplished by the use of a @State
variable, in the parent view, and a @Binding
variable, in the child view.
struct PlayButton: View {
@Binding var isPlaying: Bool
var body: some View {
Button(isPlaying ? "Pause" : "Play") {
isPlaying.toggle()
}
}
}
Then you can define a player view that declares the state and creates a binding to the state using the dollar sign prefix:
struct PlayerView: View {
var episode: Episode
@State private var isPlaying: Bool = false
var body: some View {
VStack {
Text(episode.title)
.foregroundStyle(isPlaying ? .primary : .secondary)
PlayButton(isPlaying: $isPlaying) // Pass a binding.
}
}
}
So, I have to how questions, if I may. How can I accomplish the same thing using a more functional approach:
-
without using
@Binding
, and -
only relying on return values to pass information.
The second one is, how can I rewrite that example using Combine
framework ?
I wonder whether the same could be done using solely functions or perhaps returning structs, or is there some alternative approach that is at least closer to my request.