Understanding and implementing protocols
Swift and UIKit
have protocols at the core of their design. You might have noticed this when you were implementing custom UIViewController
transitions, or when you worked on a table view or collection view. When you implement these features, you create objects that function as delegates for the transitions, table views, and collection views and conform them to specific protocols. When you worked on view controller transitions in Chapter 5, Immersing Your Users with Animation, we also implemented an NSObject
subclass that conformed to UIViewControllerAnimatedTransitioning
.
With that said, let's take a deeper look at how we would handle creating and designing our own protocols to use in our Swift app.
Defining your own protocols
Protocols are not confined to delegate behavior only. Defining a protocol is very similar to defining a class, struct, or enum. The main difference is that a protocol does not implement or store any values...