title: Swift Array Pass by Value...same memory address?
body: <p>Can someone please clear this up for me.</p>
  
  <p>I understand (thought) <code>Swift</code> passes arrays <strong>by value</strong> as it is a struct.</p>
  
  <p>But when I pass an array via segue to the next view controller it appears to me that it is passing by reference, as when I check the memory address of the array they are the same.</p>
  
  <p>This is how I'm checking</p>
  
  <pre><code>println("\(unsafeAddressOf(runs))") // 0x0000000174240c00
  </code></pre>
  
  <p>I would have thought these memory addresses would be different ? Or am I just confusing myself.</p>
  
  <p>The Run / StaffTask classes both inherit from NSObject for saving purposes.</p>
  
  <pre><code>class Run: NSObject, NSCoding { } 
  </code></pre>
  
  <p>Furthermore, if I access a item in the array</p>
  
  <pre><code>var service = self.staffTasks[indexPath.row]
  </code></pre>
  
  <p>and edit the value, both the service variable value and the element in the array are updated. They have the same memory address, as shown by</p>
  
  <pre><code>println("\(unsafeAddressOf(service))  \(unsafeAddressOf(self.staffTasks[indexPath.row]))")
  </code></pre>
  
  <p>Also...</p>
  
  <p>staffTasks are a subset of a larger array called runs</p>
  
  <p>When I search for the service object, with the larger set, I find that they are also the same memory address</p>
  
  <pre><code>if let index = find(self.runs, self.staff) {
      println("local \(unsafeAddressOf(self.staff))  main list \(unsafeAddressOf(self.runs[index]))")
  }
  </code></pre>
  
  <p>I am using NSCoding to save my objects, so I thought I would need to find the service object within the larger set, replace that object and then save them out. </p>
  
  <p>But turns out I don't need to, I am able to edit the service variable and array of runs is updated on automatically ... like a reference type would be.</p>
  
  <p>Passing? .. setting the Var</p>
  
  <p>in prepare for segue, just setting the local var in the second VC using the standard way, var runsInSecondVC = runs. Not using, &amp;pointers or any other weirdness.</p>
  
  <p>Thanks for any help.</p>
  
  <p>Class Basic Details </p>
  
  <pre><code>class Run: NSObject, NSCoding {
  
      var runDate:NSDate!
      var staffName:String!
      var staffEmail:String!
      var runTasks:[StaffTask]!
  
  }
  
  class StaffTask: NSObject, NSCoding {
  
      var taskName:String
      var taskTime:NSInteger
      var clientName:String!
  }
  </code></pre>
  
  <p>These are the basic of these classes. Nothing complicated. </p>