How can I make a weak protocol reference in 'pure' Swift (without @objc)

The Solution to How can I make a weak protocol reference in 'pure' Swift (without @objc) is


You need to declare the type of the protocol as AnyObject.

protocol ProtocolNameDelegate: AnyObject {
    // Protocol stuff goes here
}

class SomeClass {
    weak var delegate: ProtocolNameDelegate?
}

Using AnyObject you say that only classes can conform to this protocol, whereas structs or enums can't.

~ Answered on 2014-06-08 08:35:30


Most Viewed Questions: