Using as a concrete type conforming to protocol AnyObject is not supported

I ran into the same problem when I tried to implement weak containers. As @plivesey points out in a comment above, this seems to be a bug in Swift 2.2 / Xcode 7.3, but it is expected to work.

However, the problem does not occur for some Foundation protocols. For example, this compiles:

let container = WeakContainer<NSCacheDelegate>()

I found out that this works for protocols marked with the @objc attribute. You can use this as a workaround:

Workaround 1

@objc
public protocol MyDelegate : AnyObject { }

let container = WeakContainer<MyDelegate>() // No compiler error

As this can lead to other problems (some types cannot be represented in Objective-C), here is an alternative approach:

Workaround 2

Drop the AnyObject requirement from the container, and cast the value to AnyObject internally.

struct WeakContainer<T> {
  private weak var _value:AnyObject?
  var value: T? {
    get {
      return _value as? T
    }
    set {
      _value = newValue as? AnyObject
    }
  }
}

protocol MyDelegate : AnyObject { }

var container = WeakContainer<MyDelegate>() // No compiler error

Caveat: Setting a value that conforms to T, but is not an AnyObject, fails.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)