title: Are there multiple copies of Global variables in Swift 4.x/5.x? If so, how do you stop that?
body: <p>Scratching my head quite a bit on this one and can't figure this out - hopefully someone can help or explain before all my hair is gone!  Any advice or solution would be most appreciative.  Apologies in advance for the length of this question</p>
  
  <p>My App keeps up with what to display in its tableView with a Global variable (ok, maybe not the best design, but that's not the point of this) an array of structures or classes, it doesn't seem to matter</p>
  
  <pre><code> class Section {
       var sectionDate : String
       var sectionData : [CeuEvent]
       var isVisible : Bool = false {
       // added these to verify isVisible is being changed.
           willSet(isVisible) {
               print("About to set isVisible to \(isVisible)")
           }
           didSet {
               if isVisible !=  oldValue  {
                   print("Added \(isVisible) - \(oldValue)")
               }
           }
       }
       init(sectionDate: String, sectionData: [CeuEvent], isVisible: Bool) {
           self.sectionDate = sectionDate
           self.sectionData = sectionData
           self.isVisible = isVisible
       }
   }
   var sections : [Section] = []
  </code></pre>
  
  <p>The tableView sections all start out as collapsed (isVisible == false)</p>
  
  <p>The tableView section header has a tappable label, with a tag value = the section number and a gesture recogniser attached.  The action for the recogniser gets invoked as expected:</p>
  
  <p>In TapGestureRecogniser sections[label.tag].isVisible is checked for the isVisible value and as expected, it is false.</p>
  
  <pre><code> (lldb) po sections
   ▿ 1 element
     ▿ 0 : &lt;Section: 0x600002181dd0&gt;
  
   (lldb) po sections[0]
   &lt;Section: 0x600002181dd0&gt;
  
   (lldb) po sections[0].isVisible
   false
  </code></pre>
  
  <p>then isVisible is toggled - and verified in the debugger</p>
  
  <pre><code> (lldb) po sections[0].isVisible
   true
  </code></pre>
  
  <p>tableView.reload() is invoked.</p>
  
  <p>In tableView(_ tableView: UITableView, numberOfRowsInSection section: Int), section[0].isVisible is tested</p>
  
  <pre><code> (lldb) po sections
   ▿ 1 element
     ▿ 0 : &lt;Section: 0x6000021d2ee0&gt;
  
   (lldb) po sections[0]
   &lt;Section: 0x6000021d2ee0&gt;
  </code></pre>
  
  <p>and it's back to being false.</p>
  
  <pre><code> (lldb) po sections[0].isVisible
   false
  </code></pre>
  
  <p>It seems the var sections:[Section] is now at a different memory address, perhaps a copy when isInvisible is updated in the TapGestureRecogniser, and back to the original when responding to numberOfRowsInSection() as indicated by their memory addresses:</p>
  
  <p>memory value of sections and section[0] in TapGestureRecogniser action after being set to true:</p>
  
  <pre><code> (lldb) po countLabel.tag
   0
  
   (lldb) po sections
   ▿ 1 element
     ▿ 0 : &lt;Section: 0x600001805d40&gt;
  
   (lldb) po sections[0]
   &lt;Section: 0x600001805d40&gt;
  
   (lldb) po sections[0].isVisible
   true
  </code></pre>
  
  <p>in numberOfRowsInSection() - isVisible is back to false</p>
  
  <pre><code> (lldb) po sections
   ▿ 1 element
     ▿ 0 : &lt;Section: 0x6000018cc480&gt;
  
   (lldb) po sections[0]
   &lt;Section: 0x6000018cc480&gt;
  
   (lldb) po sections[0].isVisible
   false
  </code></pre>
  
  <p>It doesn't matter if var sections is wrapped in a struct and made static or even added to AppDelegate and accessed appropriately - the isVisible value keeps changing back to false.</p>