Swift version: 5.6
Delegates are extremely common in iOS development, but fortunately they are easy to understand: a delegate is any object that should be notified when something interesting has happened. What that "something interesting" means depends on the context: for example, a table view's delegate gets notified when the user taps on a row, whereas a navigation controller's delegate gets notified when the user moves between view controllers.
When you agree to be the delegate for an object, you will almost certainly need to conform to a specific protocol, such as
UITableViewDelete. These protocols will usually have some optional methods that you can implement if you care when something happens, for example, table views can notify you when users deselect a row, but most developers don't care. These protocols may also have some required methods that you must implement.
SPONSORED Build Chat messaging quickly with Stream Chat. The Stream iOS Chat SDK is highly flexible, customizable, and crazy optimized for performance. Take advantage of this top-notch developer experience, get started for free today!
Available from iOS 2.0 – see Hacking with Swift tutorial 1
This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.
Link copied to your pasteboard.