logo资料库

objc.io - Advanced Swift_Swift 5.pdf

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