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

前端 未结 6 1333
时光取名叫无心
时光取名叫无心 2020-12-05 00:12

I\'m using Swift 2 and using WeakContainer as a way to store a set of weak objects, much like NSHashTable.weakObjectsHashTable()

struct WeakCont         


        
6条回答
  •  误落风尘
    2020-12-05 01:01

    Your issue is that WeakContainer requires its generic type T to be a subtype of AnyObject - a protocol declaration is not a subtype of AnyObject. You have four options:

    1. Instead of declaring WeakContainer replace it with something that actually implements MyDelegate. The Swift-y approach for this is to use the AnyX pattern: struct AnyMyDelegate : MyDelegate { ... }

    2. Define MyDelegate to be 'class bound' as protocol MyDelegate : class { ... }

    3. Annotate MyDelegate with @obj which, essentially, makes it 'class bound'

    4. Reformulate WeakContainer to not require its generic type to inherit from AnyObject. You'll be hard pressed to make this work because you need a property declared as weak var and there are limitation as to what types are accepted by weak var - which are AnyObject essentially.

提交回复
热议问题