Advanced Swift Updated for Swift 5

This document was uploaded by one of our users. The uploader already confirmed that they had the permission to publish it. If you are author/publisher or own the copyright of this documents, please report to us by using this DMCA report form.

Simply click on the Download Book button.

Yes, Book downloads on Ebookily are 100% Free.

Sometimes the book is free on Amazon As well, so go ahead and hit "Search on Amazon"

Author(s): Chris Eidhof
Series: Version 4
Publisher: objc.io
Year: 2019

Language: English
Pages: 431

Introduction......Page 10
Who Is This Book For?......Page 12
Themes......Page 13
Terminology......Page 16
Swift Style Guide......Page 20
Revision History......Page 22
Built-In Collections......Page 24
Arrays and Mutability......Page 25
Array Indexing......Page 27
Transforming Arrays......Page 29
Array Slices......Page 41
Dictionaries......Page 43
Some Useful Dictionary Methods......Page 44
Hashable Requirement......Page 46
Sets......Page 47
Set Algebra......Page 48
Index Sets and Character Sets......Page 49
Using Sets Inside Closures......Page 50
Ranges......Page 51
Countable Ranges......Page 52
Range Expressions......Page 53
Recap......Page 54
Optionals......Page 56
Sentinel Values......Page 57
Replacing Sentinel Values with Enums......Page 59
if let......Page 61
while let......Page 62
Doubly Nested Optionals......Page 64
if var and while var......Page 65
Scoping of Unwrapped Optionals......Page 66
Optional Chaining......Page 70
The nil-Coalescing Operator......Page 73
Using Optionals with String Interpolation......Page 75
Optional map......Page 77
Optional flatMap......Page 79
Filtering Out nils with compactMap......Page 80
Equating Optionals......Page 81
When to Force-Unwrap......Page 84
Improving Force-Unwrap Error Messages......Page 86
Asserting in Debug Builds......Page 87
Implicitly Unwrapped Optionals......Page 89
Recap......Page 90
Functions......Page 91
Overview......Page 92
Flexibility through Functions......Page 99
Functions as Data......Page 103
Delegates, Cocoa Style......Page 110
Delegates That Work with Structs......Page 111
Functions Instead of Delegates......Page 113
inout Parameters and Mutating Methods......Page 116
When & Doesn't Mean inout......Page 119
Properties......Page 120
Change Observers......Page 121
Lazy Stored Properties......Page 123
Subscripts......Page 125
Advanced Subscripts......Page 126
Key Paths......Page 128
Key Paths Can Be Modeled with Functions......Page 130
Writable Key Paths......Page 131
The Key Path Hierarchy......Page 134
Autoclosures......Page 135
The @escaping Annotation......Page 138
withoutActuallyEscaping......Page 140
Recap......Page 141
Structs and Classes......Page 142
Value Types and Reference Types......Page 143
Mutation......Page 147
Mutating Methods......Page 151
inout Parameters......Page 152
Reference Cycles......Page 153
Closures and Reference Cycles......Page 158
Choosing between Unowned and Weak References......Page 160
Deciding between Structs and Classes......Page 161
Classes with Value Semantics......Page 162
Structs with Reference Semantics......Page 163
Copy-On-Write Optimization......Page 165
Copy-On-Write Tradeoffs......Page 166
Implementing Copy-On-Write......Page 167
Recap......Page 171
Enums......Page 173
Overview......Page 174
Enums Are Value Types......Page 175
Sum Types and Product Types......Page 177
Pattern Matching......Page 179
Pattern Matching in Other Contexts......Page 183
Designing with Enums......Page 185
Switch Exhaustively......Page 186
Make Illegal States Impossible......Page 187
Use Enums for Modeling State......Page 191
Choosing between Enums and Structs......Page 195
Parallels between Enums and Protocols......Page 197
Use Enums to Model Recursive Data Structures......Page 200
Raw Values......Page 206
The RawRepresentable Protocol......Page 207
Manual RawRepresentable Conformance......Page 208
RawRepresentable for Structs and Classes......Page 209
Internal Representation of Raw Values......Page 210
Enumerating Enum Cases......Page 211
Manual CaseIterable Conformance......Page 212
Frozen and Non-Frozen Enums......Page 213
Tips and Tricks......Page 215
Recap......Page 220
Strings......Page 221
Unicode......Page 222
Combining Marks......Page 224
Emoji......Page 227
Strings and Collections......Page 230
Bidirectional, Not Random Access......Page 231
Range-Replaceable, Not Mutable......Page 232
String Indices......Page 233
Substrings......Page 237
StringProtocol......Page 239
Code Unit Views......Page 242
Index Sharing......Page 244
Strings and Foundation......Page 245
Other String-Related Foundation APIs......Page 247
Ranges of Characters......Page 249
CharacterSet......Page 250
Unicode Properties......Page 251
Internal Structure of String and Character......Page 252
String Literals......Page 254
String Interpolation......Page 255
Custom String Descriptions......Page 258
Text Output Streams......Page 259
Recap......Page 263
Generics......Page 265
Generic Types......Page 266
Extending Generic Types......Page 268
Generics vs. Any......Page 270
Designing with Generics......Page 272
Recap......Page 275
Protocols......Page 276
Protocol Witnesses......Page 278
Conditional Conformance......Page 280
Protocol Inheritance......Page 281
Designing with Protocols......Page 282
Protocol Extensions......Page 283
Customizing Protocol Extensions......Page 284
Protocol Composition......Page 287
Protocols with Associated Types......Page 288
Conditional Conformance with Associated Types......Page 290
Existentials......Page 292
Existentials and Associated Types......Page 293
Type Erasers......Page 294
Retroactive Conformance......Page 296
Recap......Page 297
Collection Protocols......Page 298
Sequences......Page 300
Iterators......Page 301
Conforming to Sequence......Page 304
Iterators and Value Semantics......Page 305
Function-Based Iterators and Sequences......Page 307
Single-Pass Sequences......Page 308
The Relationship between Sequences and Iterators......Page 310
Conforming List to Sequence......Page 311
Collections......Page 313
A Custom Collection......Page 314
Array Literals......Page 320
Associated Types......Page 321
Indices......Page 322
Index Invalidation......Page 324
Advancing Indices......Page 325
Custom Collection Indices......Page 326
Subsequences......Page 329
Slices......Page 331
Subsequences Share Indices with the Base Collection......Page 333
Specialized Collections......Page 334
BidirectionalCollection......Page 335
RandomAccessCollection......Page 336
MutableCollection......Page 337
RangeReplaceableCollection......Page 339
Lazy Sequences......Page 341
Lazy Processing of Collections......Page 343
Recap......Page 344
Error Handling......Page 345
Error Categories......Page 346
The Result Type......Page 348
Throwing and Catching......Page 350
Typed and Untyped Errors......Page 352
Non-Ignorable Errors......Page 354
Converting between throws and Optionals......Page 356
Converting between throws and Result......Page 357
Chaining Errors......Page 358
Chaining Result......Page 359
Errors in Asynchronous Code......Page 361
Cleaning Up Using defer......Page 364
Rethrowing......Page 365
Bridging Errors to Objective-C......Page 367
Recap......Page 369
Encoding and Decoding......Page 371
Automatic Conformance......Page 373
Encoding......Page 374
The Encoding Process......Page 376
Containers......Page 377
Coding Keys......Page 380
The encode(to:) Method......Page 381
Manual Conformance......Page 382
Custom Coding Keys......Page 383
Custom encode(to:) and init(from:) Implementations......Page 384
Common Coding Tasks......Page 386
Making Types You Don't Own Codable......Page 387
Making Classes Codable......Page 390
Making Enums Codable......Page 396
Decoding Polymorphic Collections......Page 398
Recap......Page 399
Interoperability......Page 401
Package Manager Setup......Page 402
Wrapping the CommonMark Library......Page 405
A Safer Interface......Page 411
An Overview of Low-Level Types......Page 418
Pointers......Page 419
Closures as C Callbacks......Page 422
Making It Generic......Page 424
Recap......Page 427
Final Words......Page 429