title: Store reference to struct in Swift
body: <p>Let's say I have a model, called <code>Car</code> with a property <code>color</code> of type <code>String</code>:</p>
  
  <pre><code>class Car {
      var color: String?
  }
  </code></pre>
  
  <p>Now, I have another class called <code>Interface</code> that I wish to use to modify the value of the <code>color</code> property of <code>Car</code>. To do this, I'd like to somehow bind <code>color</code> to a property of <code>Interface</code>, such as <code>key</code>:</p>
  
  <pre><code>class Interface {
  
      var storedKey: String?
  
      func bind(key: String?) {
          self.storedKey = key
      }
  }
  </code></pre>
  
  <p>Usage:</p>
  
  <pre><code>let corolla = Car()
  corolla.color = "green"
  let interface = Interface()
  interface.bind(key: &amp;corolla.color)
  interface.storedKey = "blue"
  print(corolla.color) // still "green"! :(
  </code></pre>
  
  <p>This approach fails because strings are value types (structs) in Swift, so the <code>bind</code> function gets a copy of <code>corolla.color</code>, not a reference. This means modifying <code>storedKey</code> of <code>interface</code> has no effect on the value of <code>corolla.color</code>.</p>
  
  <p>If we add <code>inout</code> to the binding function as shown below, we basically obtain a reference to the key passed in (<a href="https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/doc/uid/TP40014097-CH34-ID545" rel="nofollow noreferrer" title="it&#39;s actually copied in then copied out">it's actually copied in then copied out</a>), but it only lasts for the scope of the function.</p>
  
  <pre><code>func bind(key: inout String?) {
      self.storedKey = key  // assignment of value type is a copy! :/
  }
  </code></pre>
  
  <p>The results are the same, modifying <code>storedKey</code> has no effect on the property it is bound to. </p>
  
  <p><strong>What this boils down to is, can we obtain a reference to a struct in Swift?</strong> </p>
  
  <hr>
  
  <p>Since structs occupy space in memory, we may be able to obtain a pointer to the key passed in, here is a working (incorrect) solution:</p>
  
  <pre><code>class Car {
      var color: String?
  }
  
  let corolla = Car()
  corolla.color = "green"
  
  class Interface {
  
      var storedKey: UnsafeMutablePointer&lt;String?&gt;?
  
      func bind(_ key: inout String?) {
          self.storedKey = withUnsafeMutablePointer(to: &amp;key) { ptr in
              return ptr
          }
      }
  }
  
  var interface = Interface()
  interface.bind(&amp;corolla.color)
  interface.storedKey?.pointee = "blue" // :O it's... working?
  print(corolla.color)
  </code></pre>
  
  <p>As I see it, this breaks at least two semantic rules in Swift:</p>
  
  <ol>
  <li><p><code>withUnsafeMutablePointer</code> converts the address of an instance passed in via <code>&amp;</code> to a <code>UnsafeMutablePointer</code> of that instances' type, to be used <strong>only within</strong> the provided closure. As per <a href="https://developer.apple.com/reference/swift/2429788-withunsafemutablepointer" rel="nofollow noreferrer" title="the docs:">the Swift docs</a>:</p>
  
  <blockquote>
    <p>The pointer argument to body is valid only for the lifetime of the closure. Do not escape it from the closure for later use.</p>
  </blockquote></li>
  <li>The second problem is that this takes advantage of a Swift compiler optimization that allows <code>bind</code> to actually obtain a reference to <code>key</code> instead of a copy. Otherwise, the pointer would not obtain a reference to the original key, but to a copy. <code>inout</code> parameters are normally implemented using a behavior known as copy-in copy-out, whereby a copy of the argument is passed in to the function, possibly modified, and then assigned back to the original argument. <a href="https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Declarations.html#//apple_ref/doc/uid/TP40014097-CH34-ID351" rel="nofollow noreferrer" title="as per the docs">As per the docs</a>, there is an optimization whereby if the argument is a stored property, the same memory location is used both inside and outside the function body. This allowed our pointer to obtain a reference to the original String struct instead of a copy, allowing this to work.
  
  <blockquote>
    <p>As an optimization, when the argument is a value stored at a physical address in memory, the same memory location is used both inside and outside the function body.</p>
  </blockquote></li>
  </ol>
  
  <p>Is there any way to store a reference to a struct such as <code>String</code>? Or am I on a wild goose chase?</p>
  
  <p>Edit 1: Also, I have no control of the types of <code>Car</code>'s properties.</p>