title: SwiftUI - Using an Array of Ints that is wrapped with @Binding
body: <p>I gather that the @Binding wrapper is used when a parent view and a child view both need to share a variable without violating the principle of having “a single source of truth”. This isn’t working as I expect when the variable is an Array. Essentially, the data that the child view assigns to this bound variable gets dropped so that the parent view sees an empty array.</p>
  <p>The attached example is taken from the Developer Documentation (under XCode’s Help menu) for the @Binding wrapper. Apple’s code shows a simple video controller. It allows a user to play or pause videos (videos are stored as <code>Episode</code> structs). The child view is named <code>PlayButton</code> and toggles between a right-arrow (start) and a double-bar (stop). In Apple’s code the child view had a single variable wrapped with @Binding, which was the <code>isPlaying</code> Boolean variable. When the play button is tapped it toggles the <code>isPlaying</code> variable. As you might expect, the orginal code worked fine.</p>
  <p>To demonstrate the problem I’ve modified the child view so that now accepts an array of Episodes.  Please assume that the child view must report the size of the show titles to the parent view without violating the single source of truth principle. As a consequence of this need, there is also a new @Binding-wrapped array that records the number-of-characters in a show’s title (for each of the shows).</p>
  <p>Additionally, the child view now shows Text Views reporting the show title of each Episode and the title’s size, just to prove that the code is being executed. The parent View should then be able to display the number of elements in its @State array, <code>sizeOfShowTitles</code>. As coded here, I expect the number of episodes to be 1. Instead, the parent view is reporting that sizeOfShowTitles has zero elements.</p>
  <p>The playground contains just these 4 elements:</p>
  <ol>
  <li>Episode (a struct that identifies the videos)</li>
  <li>PlayButton (the child View)</li>
  <li>PlayerView (the parent View)</li>
  <li>the PlaygroundPage.current.setLiveView(PlayerView()) command, used to excuted SwiftUI in playgrounds.</li>
  </ol>
  <p>Can anyone comment on why assignment of values to the bound array is failing?</p>
  <p>Note 1:
  The problem does not seem to lie with the <code>.append</code> function used in the child view. If you replace the append function with a simple assignment then the outcome is the same - there are no elements in the array in the parent View (tested, but not shown here).</p>
  <p>Note 2.
  As shown in Apples code (and retained here), assigning a value to a Boolean Type works as expected. So, it appears that the problem has something to do with the Array Type.</p>
  <p>Note 3.
  I’m using Swift 5.5 in XCode 5.4.1.</p>
  <pre><code>// Playground used to show the issue with saving a
  // value to an array that is wrapped with @Binding
  
  import SwiftUI
  import PlaygroundSupport
  
  // HELPER Struct: records metadata on shows that can be played in PlayerView
  struct Episode: Identifiable {
      var id = UUID()
      var title = &quot;Title&quot;
      var showTitle = &quot;Show Title&quot;
  }
  
  // CHILD View
  //  String Array)
  struct PlayButton: View {
  
      // input API
      @Binding var isPlaying: Bool
      var episodes: [ Episode ]
  
      // output API
      @Binding var charactersInShowTitle: [ Int ]
  
  
      var body: some View {
          Button(action: {
              self.isPlaying.toggle()
          }) {
              Image(systemName: isPlaying ? &quot;pause.circle&quot; : &quot;play.circle&quot;)
          }
      
          ForEach( episodes ) { episode in
              Text( &quot;CHILD: \( analyzeShowTitle( episode) )&quot; )
          }
      }
  
  func analyzeShowTitle( _ episode: Episode ) -&gt; String {
  
      let characterCount = episode.showTitle.count
      charactersInShowTitle.append( characterCount )
      return &quot;\( episode.showTitle ) - \( characterCount ) chars&quot;
      }
  }
  
  // PARENT
  // (modified to show the list of sizes from a String Array)
  struct PlayerView: View {
      let episodes = [ Episode(title: &quot;Title 1&quot;,
                               showTitle: &quot;Show 1&quot;),
                       Episode( title: &quot;Title 1&quot;,
                                showTitle: &quot;Show 21&quot;)
                     ]
      @State private var isPlaying: Bool = false
      @State private var sizeOfShowTitles = [ Int ]()
  
      var body: some View {
          VStack {
              Text(&quot;Player App&quot;)
                  .foregroundStyle(isPlaying ? .primary : .secondary)
              Text(&quot;&quot;)
              PlayButton(isPlaying: $isPlaying,
                         episodes: episodes,
                         charactersInShowTitle: $sizeOfShowTitles )
              Text(&quot;&quot;)
              Text( &quot;PARENT no. elements: \( $sizeOfShowTitles.wrappedValue.count)&quot;
                  )
          }
          .frame(width: 300)
      }
  }
  
  PlaygroundPage.current.setLiveView(PlayerView())
  </code></pre>