- Swift 4 Programming Cookbook
- Keith Moon
- 543字
- 2021-07-08 10:21:29
How it works...
Let's take a look at the different types of closures we just implemented:
// No input, no output
let printAuthorsDetails: () -> Void = {
let name = PersonName(givenName: "Keith", middleName: "David", familyName: "Moon")
let author = Person(name: name)
print(author.displayString)
}
As a first-class type in Swift, closures can be assigned to constants or variables, and constants and variables need a type. To define a closure's type, we need to specify the input parameter types and the output type, and for the closure in the preceding code, the type is () -> Void. The Void type is another way of saying nothing, so this closure takes no inputs and returns nothing, and the closure's functionality is defined within the curly brackets, as with other functions.
Now that we have this closure defined and assigned to the printAuthorsDetails constant, we can execute it as follows, which will cause this author's details to be printed:
printAuthorsDetails() // "Keith David Moon - Location: UK"
The next closure type takes no input parameters, but returns a Person object, as you can see with the () -> Person type definition:
// No input, Person output
let createAuthor: () -> Person = {
let name = PersonName(givenName: "Keith", middleName: "David", familyName: "Moon")
let author = Person(name: name)
return author
}
let author: Person = createAuthor()
print(author.displayString) // "Keith David Moon - Location: UK"
Since it has an output, the execution of the closure returns a value and can be assigned to a variable or constant. In the preceding code, we execute the createAuthor closure and assign the output to the author constant. Since we defined the closure type as () -> Person, the compiler knows that the output type is a Person and therefore the type of the constant can be inferred, so we don't need to explicitly declare it. Let's remove the explicit type declaration:
let author = createAuthor()
print(author.displayString) // "Keith David Moon - Location: UK"
Next, let's take a look at a closure that takes input parameters:
// String inputs, no output
let printPersonsDetails: (String, String, String) -> Void = { given, middle, family in
let name = PersonName(givenName: given, middleName: middle, familyName: family)
let author = Person(name: name)
print(author.displayString)
}
printPersonsDetails("Kathleen", "Mary", "Moon") // "Kathleen Mary Moon - Location: UK"
You will remember, from the recipe on functions, that we can define parameter labels, which define how the parameters are referenced when the function is used, and parameter names, which define how the parameter is referenced from within the function. In closures, these are defined a bit differently:
- Parameter labels cannot be defined for closures, so, when calling a closure, the order and parameter type have to be used to determine what values should be provided as parameters:
(String, String, String) -> Void
- Parameter names are defined inside the curly brackets, followed by the in keyword:
given, middle, family in
Putting it all together, we can define and execute a closure with inputs and an output, as follows:
// String inputs, Person output
let createPerson: (String, String, String) -> Person = { given, middle, family in
let name = PersonName(givenName: given, middleName: middle, familyName: family)
let person = Person(name: name)
return person
}
let melody = createPerson("Melody", "Margaret", "Moon")
print(melody.displayString) // "Melody Margaret Moon - Location: UK"
- Getting Started with Citrix XenApp? 7.6
- Kali Linux Web Penetration Testing Cookbook
- Microsoft Exchange Server PowerShell Cookbook(Third Edition)
- Spring Cloud Alibaba微服務架構設計與開發實戰
- Python自然語言處理實戰:核心技術與算法
- MySQL 8 DBA基礎教程
- 青少年美育趣味課堂:XMind思維導圖制作
- HTML5+CSS3網頁設計
- Linux Device Drivers Development
- 精通Python自動化編程
- Visual FoxPro程序設計習題集及實驗指導(第四版)
- jQuery Mobile移動應用開發實戰(第3版)
- Mastering Linux Security and Hardening
- Web Developer's Reference Guide
- Mastering HTML5 Forms