title: type A requires that type B be a class type swift 4
body: <p>the following code gives me a compile error </p>
  
  <blockquote>
    <p>'WeakReference' requires that 'ServiceDelegate' be a class type</p>
  </blockquote>
  
  <pre><code>protocol ServiceDelegate: AnyObject {
      func doIt()
  }
  
  class SomeClass() {
      // compile error occurs in this line
      private var observers = [WeakReference&lt;ServiceDelegate&gt;]()
  }
  </code></pre>
  
  <p>WeakReference code:</p>
  
  <pre><code>final class WeakReference&lt;T: AnyObject&gt; {
  
      private(set) weak var value: T?
  
      init(value: T?) {
          self.value = value
      }
  }
  </code></pre>
  
  <p>How can I fix this error? Delegate should be declared correctly as per this <a href="https://useyourloaf.com/blog/class-only-protocols-in-swift-4/" rel="noreferrer">site</a>.</p>
  
  <p>What I have tried so far:</p>
  
  <ul>
  <li>Changing the delegate protocol conformance from <code>AnyObject</code> to
  <code>class</code> does not solve the problem.</li>
  <li>Try the above code in a clean playground.</li>
  </ul>