Delegation and Protocols are some of the most important programming patterns in Swift. Every iOS application has at least a few custom delegations. Moreover, almost all built-in frameworks benefit from protocols and delegations.
Delegation and Protocols: Protocols
A protocol as the name implies is the blueprint of methods, properties, and other requirements that handle a certain task. Are a result, they are then adopted by class, structure, or enumeration to do the actual work. There are different approaches for using protocols.
Unlike Objective-C, Swift doesn’t have optional functions in a protocol. If you need an optional function, you must leave its body empty.
The protocol only specifies the required property name and type. Not whether it should be stored or computed. As a result, protocols contain no logic, they are simply definitions of methods, no implementations.
Text Field Delegates
In setting delegations for outlets, you could either add them by code through the .delegate file. You could also do it in the storyboard by dragging over the view controller outlet. A text field, for instance, has many different delegates. That is to say it means many different protocols are being conformed in it.
Delegation and Protocols: Delegations
Delegation is a very commonly used pattern in iOS development. The delegate design pattern is a pattern that uses protocols. That is to say, that in delegation pattern an object delegates some of its behaviour or control to another object using a protocol. This is often useful when an object needs to communicate information or request information from another object.
Interested in Learning More?
If you want to learn more, join my Complete Mobile Developer Courses.