title: Swift protocol & weak references with class
body: <p>If I have a protocol:</p>
  
  <pre><code>protocol SomeProtocol {
      func doSomething()
  }
  </code></pre>
  
  <p>and in a helper class, I have a reference to a protocol variable:</p>
  
  <pre><code>class someClass {
      var delegate: SomeProtocol? 
  }
  </code></pre>
  
  <p>because <code>SomeProtocol</code> isn't marked with <code>: class</code>, it's assumed that <code>delegate</code> can be anything and in the case of value types (structs and enums) there's no need for <code>weak var</code> because value types can't create strong references. In fact, the compiler doesn't allow <code>weak var</code> on anything but class types. </p>
  
  <p>However, nothing stops you from setting a class as the delegate and if the protocol isn't marked with <code>: class</code> (as <code>SomeProtocol is),</code>weak var` can't be used and that creates a retain cycle. </p>
  
  <pre><code>class MyClass: NSObject, SomeProtocol {
      func doSomething() { }
  }
  
  struct MyStruct: SomeProtocol {
      func doSomething() { }
  }
  
  let someClass = SomeClass()
  let myStruct = MyStruct()
  someClass.delegate = myStruct 
  // After myStruct gets assigned to the delegate, do the delegate and the struct refer to the same instance or does the struct get copied?D
  
  let myClass = MyClass()
  someClass.delegate = myClass // can't use weak var so myClass is retained
  </code></pre>
  
  <p>Given the above example, in the case of using delegates and datasource, shouldn't <code>: class</code> always be used? Basically any protocol that is used to maintain a reference should always be restricted to class objects right?</p>