Thêm nhiều đại biểu thay vì chỉ một đại biểu là một nhiệm vụ khá phổ biến. Giả sử chúng ta có giao thức và một lớp:Sử dụng NSHashTable để triển khai mẫu Observer trong Swift 3
protocol ObserverProtocol
{
...
}
class BroadcasterClass
{
// Error: Type 'ObserverProtocol' does not conform to protocol 'AnyObject'
private var _observers = NSHashTable<ObserverProtocol>.weakObjects()
}
Nếu chúng ta cố gắng ép buộc ObserverProtocol
cho phù hợp AnyObject
giao thức, chúng tôi sẽ nhận được một lỗi:
Using 'ObserverProtocol' as a concrete type conforming to protocol 'AnyObject' is not supported
Là nó thậm chí có thể tạo ra một tập hợp các đại biểu yếu trong Swift 3.0?