Swift version: 5.4
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 Join a FREE crash course for iOS devs who want to become complete senior developers — from October 18th to 24th. Learn how to apply iOS app architecture patterns through a series of lectures and practical coding sessions.
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.