Swift version: 5.0
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.
SPONSOR Tired of wasting time debugging your Swift app? Instabug’s SDK is here to help you minimize your debugging time by providing you with complete device details, network logs, and reproduction steps with every bug report. All data is attached automatically. It only takes a line of code to setup. Get started for free.
Available from iOS 2.0 – see Hacking with Swift tutorial 1
Did this solution work for you? Please pass it on!
Other people are reading…
About the Swift Knowledge Base
This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.