Type constraint on AnyObject or Any (AnyObject or Any that conforms to a protocol)

I have since found a better way of doing this in this answer.

Updated for recent Swift versions

As I mentioned in my comment on CjCoax's answer, prefixing the protocol with @objc prevents passing Swift object types (such as enums and structs) to delegate methods.

However, using : AnyObject will allow this behaviour while allowing the protocol to be used in a weak variable, though this method is not without its limitations. You can only make classes conform to any protocol that is marked with : AnyObject. I believe that this is a better trade-off than @objc provides.

protocol MyProtocol: AnyObject {
    ...
}

I believe the recommended solution would be a class-only protocol. From the Swift Programming Language protocol page:

You can limit protocol adoption to class types (and not structures or enumerations) by adding the AnyObject protocol to a protocol’s inheritance list.

So in your example, your protocol definition would look like:

protocol RWOverlaySelectionDelegate: AnyObject {
    func areaSelected(view: UIView, points: NSArray)
}

With the addition of the AnyObject keyword, the compiler should no longer complain.


Since swift 4 instead of class we should use AnyObject. From the Swift Programming Language protocol page:

You can limit protocol adoption to class types (and not structures or enumerations) by adding the AnyObject protocol to a protocol’s inheritance list.

protocol SomeClassOnlyProtocol: AnyObject, SomeInheritedProtocol {
    // class-only protocol definition goes here
}

Tags:

Swift