title: Swift Generics - Changing Values and Passing Between View Controllers
body: <p>I'd like to use a variable of type <code>Any</code> in order to pass different classes to a child view controller. For example, I might have Table, Chair and Plate objects. In my child view controller, I'd like to change the value of one of their properties (e.g. Table.legs was 4, change that to 3), and for the parent view controller to be able to read that from the child VC. I'll use a Protocol to update the parent VC that can pop the child after reading the updated object.</p>
  
  <p>In order to work out how the passing of generics might work, I wrote this code in a playground:</p>
  
  <pre><code>class Table {
      var legs: Int
      var material: String
      init(legs: Int, material: String) {
          self.legs = legs
          self.material = material
      }
  }
  var anObject: Any?
  
  // set up the Table
  let aTable = Table(legs: 4, material: "Oak")
  
  // set anObject to be the Table
  anObject = aTable
  
  // get the object and change it
  let bTable = anObject as! Table
  bTable.legs = 3
  
  // get the original object and cast it as a Table
  let cTable = anObject as! Table
  
  print(cTable.legs) // prints 3
  </code></pre>
  
  <p>I believe from this, I should be able to do what I describe above without any issues, because the original object reference (<code>anObject</code>) is updated whenever I update a variable referencing it.</p>
  
  <p>My question is this - are there any pitfalls I should be aware of when adopting this approach? It appears that rather than creating a copy of an object, swift will always create a pointer to the original object; are there any situations when that does not hold true?</p>
  
  <p>Appologies if this is seen as a fairly basic question, but this is all fairly new to me - many thanks in advance!</p>