5.10 Closures
Closures are a powerful feature in Swift; in a nutshell, they are self-contained blocks of code that can be passed around and executed later. In the sense that closures can act like executable variables, they are similar to function types—but though their functionality overlaps, their typical use cases differ.
Function types are ideal when you need a standardized signature for multiple functions that can substitute for each other in different cases. Each compliant function must conform to the function type like a protocol/interface.
Closures, on the other hand, are ideal when you need an immediate implementation without the necessity of a standardized signature. Although closures can conform to a function type, that’s not necessarily ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access