@State and @Binding error?

I've been trying to implement the example used in the WWDC session 226 - "Data flow through SwiftUI"and seem to be having a problem that doesn't correspond to the examples shown in the Video...


I get the error:


ContentView.swift:21:13: 'PlayButton.Type' is not convertible to '(Binding<Bool>) -> PlayButton'


The funny thing is it worked at first but stopped working spontaneously..


Any help solving this or even just confirmation it is (or isn't) a common problem is appreciated.



Here's my ContentView.Swift :


import SwiftUI

struct ContentView : View {
  
    @State private var isPlaying: Bool = false

    var body: some View {
      
        VStack {
            Text(isPlaying ? "Playing" : "Not Playing")
            PlayButton()
            Image(systemName: isPlaying ? "pause.circle" : "play.circle")
        }
    }
}

#if DEBUG
struct ContentView_Previews : PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
#endif



and the (simplified) play button code:


import SwiftUI

struct PlayButton : View {
   
    @Binding  var isPlaying: Bool

    var body: some View {
        Button(action: { self.isPlaying.toggle() }) {
            Text( isPlaying ? "Stop" : "Play")
           
        }
    }
}


#if DEBUG
struct PlayButton_Previews : PreviewProvider {
    static var previews: some View {
        PlayButton(isPlaying: Binding)
    }
}
#endif
Accepted Answer

Ok, so I was being dumb.


You have to pass the @State variable (with a $ specifically to identofy the reference to the State variable) to the sub-view.


Maybe the error messages could be a little less arcane? Or maybe I could be less dumb !


This is working:


import SwiftUI
import Combine


struct ContentView : View {
   
    @State private var isPlaying: Bool = false

    var body: some View {
       
        VStack {
            Text(isPlaying ? "Playing" : "Not Playing")
            PlayButton(isPlaying: $isPlaying)
            Image(systemName: isPlaying ? "pause.circle" : "play.circle")
        }
    }
}

#if DEBUG
struct ContentView_Previews : PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
#endif
&#64;State and &#64;Binding error?
 
 
Q