Class Only Protocols In Swift 4

The delegation pattern is common in many of Apple’s Cocoa API’s. When using it with Swift you create a class-only protocol that the delegate adopts. What I had not noticed was a subtle change in the way you declare a class-only protocol in Swift 4.

Class-Only Protocols

If you want a recap on using the delegate pattern see Quick Guide to Swift Delegates. Since Swift 4 the preferred way to declare a class-only protocol is to use AnyObject rather than class. So instead of writing this:

This proposal merges the concepts of class and AnyObject, which now have the same meaning: they represent an existential for classes.

Prior to Swift 4 there were some subtle differences between using class and AnyObject but that is no longer the case. I don’t know if AnyObject makes the intent any clearer than class but the result is the same:

The protocol can only be adopted by a class (reference semantics). It cannot be adopted by a struct or enum (value semantics).

What Should I Do

Get into the habit of using AnyObject when creating class-only protocols. Using class is not yet deprecated but I assume it will be in a future version of Swift.