Expert Swift. (Ehab Amer, Marin Benčević etc.) (Z-Library)
Author: Ehab Amer, Marin Benčević, Ray Fix & Shai Mishali
商业
No Description
📄 File Format:
PDF
💾 File Size:
38.2 MB
16
Views
0
Downloads
0.00
Total Donations
📄 Text Preview (First 20 pages)
ℹ️
Registered users can read the full content for free
Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.
📄 Page
1
(This page has no text content)
📄 Page
2
Expert Swift Ehab Amer, Marin Benčević, Ray Fix & Shai Mishali Copyright ©2021 Razeware LLC. Notice of Rights All rights reserved. No part of this book or corresponding materials (such as text, images, or source code) may be reproduced or distributed by any means without prior written permission of the copyright owner. Notice of Liability This book and all corresponding materials (such as source code) are provided on an “as is” basis, without warranty of any kind, express of implied, including but not limited to the warranties of merchantability, fitness for a particular purpose, and noninfringement. In no event shall the authors or copyright holders be liable for any claim, damages or other liability, whether in action of contract, tort or otherwise, arising from, out of or in connection with the software or the use of other dealing in the software. Trademarks All trademarks and registered trademarks appearing in this book are the property of their own respective owners. Expert Swift raywenderlich.com 2
📄 Page
3
Table of Contents: Overview Book License 10............................................................................................. Before You Begin 11................................................................ What You Need 12........................................................................................ Book Source Code & Forums 13............................................................. Introduction 17.............................................................................................. Section I: Core Concepts 19.................................................. Chapter 1: Introduction 20........................................................... Chapter 2: Types & Mutation 41................................................. Chapter 3: Protocols 70................................................................. Chapter 4: Generics 100................................................................ Section II: Standard Library 118.......................................... Chapter 5: Numerics & Ranges 119.......................................... Chapter 6: Sequences, Collections & Algorithms 163....... Chapter 7: Strings 191.................................................................... Chapter 8: Codable 210................................................................. Chapter 9: Unsafe 252.................................................................... Section III: Techniques 274................................................... Chapter 10: Higher-Order Functions 275.............................. Chapter 11: Functional Reactive Programming 300.......... Chapter 12: Objective-C Interoperability 340..................... Chapter 13: Instrumentation 376.............................................. Expert Swift raywenderlich.com 3
📄 Page
4
Chapter 14: API Design Tips & Tricks 418.............................. Conclusion 450.............................................................................................. Expert Swift raywenderlich.com 4
📄 Page
5
Table of Contents: Extended Book License 10. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Before You Begin 11. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . What You Need 12. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Book Source Code & Forums 13. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . About the Authors 15. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . About the Editors 16. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . About the Artist 16. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Introduction 17. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . How to read this book 18. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Section I: Core Concepts 19. . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 1: Introduction 20. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Swift released 21. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Easy onboarding 21. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Multi-paradigm 22. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The Swift compiler 25. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Implementing a language feature 30. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 39. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 40. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 2: Types & Mutation 41. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The fundamental types 42. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Modeling with types 42. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Functions and methods 48. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Exercises 58. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . QuadTree 58. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 68. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 3: Protocols 70. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Expert Swift raywenderlich.com 5
📄 Page
6
Getting started with protocols 71. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Behind the scenes of protocols 77. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Protocols and the type system 81. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Synthesized protocol conformance 83. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Protocol-oriented programming 84. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 99. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 99. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 4: Generics 100. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Getting started with generics 101. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Creating a generic networking library 108. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 117. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 117. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Section II: Standard Library 118. . . . . . . . . . . . . . . . . . . . . . . Chapter 5: Numerics & Ranges 119. . . . . . . . . . . . . . . . . . . . . . . . . . . . . Representing numbers 120. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Integers 121. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Floating-point 133. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Full generic programming with floating-point 141. . . . . . . . . . . . . . . . . . . . . . . Ranges 157. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 161. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 162. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 6: Sequences, Collections & Algorithms 163. . . . . . . . . . A family of protocols 164. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Sequences and collections 165. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Iterators and sequences 166. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Collections 171. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Generic algorithms 186. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 188. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 190. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Expert Swift raywenderlich.com 6
📄 Page
7
Chapter 7: Strings 191. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Binary representations 192. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Human representation 195. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Grapheme cluster 195. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . UTF in Swift 197. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . String and Substring in memory 201. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Custom string interpolation 202. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 209. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 8: Codable 210. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . What is Codable? 211. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . What you’ll learn, and what you won’t 212. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Brushing up on the basics 212. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . API #1: Ray’s Books 213. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key decoding strategies 215. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Data decoding strategies 216. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . API #2: Magic: The Gathering 221. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . API #3: Alpha Vantage 232. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Encoding 244. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 251. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 9: Unsafe 252. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Definition of unsafe & undefined behaviors 253. . . . . . . . . . . . . . . . . . . . . . . . . What is a pointer? 254. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Memory layout 255. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Trivial types 257. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Pointer types 260. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Memory binding 265. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Unsafe operations 270. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 273. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 273. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Section III: Techniques 274. . . . . . . . . . . . . . . . . . . . . . . . . . . . Expert Swift raywenderlich.com 7
📄 Page
8
Chapter 10: Higher-Order Functions 275. . . . . . . . . . . . . . . . . . . . . . A simple text printer 276. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . First-order functions 277. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Your first higher-order function 278. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Higher-order functions in the standard library 281. . . . . . . . . . . . . . . . . . . . . Function as a return type 290. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 299. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 299. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 11: Functional Reactive Programming 300. . . . . . . . . . . . Functional? Reactive? 301. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Reactive basics 304. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The Luthier app 307. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Checkout 317. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Performing Checkout 327. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 339. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 339. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 12: Objective-C Interoperability 340. . . . . . . . . . . . . . . . . . What you’ll learn 341. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Getting started 341. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Bridging and umbrella headers 342. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Making the app launch 343. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Enriching FeedItem 345. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Objective-C and … SwiftUI ?! 353. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 374. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 375. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 13: Instrumentation 376. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Getting started 377. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Memory optimization 377. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Memory leaks 381. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Performance 387. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Expert Swift raywenderlich.com 8
📄 Page
9
Core Data 393. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Build times 398. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Timelane 403. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Signpost 411. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 417. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 14: API Design Tips & Tricks 418. . . . . . . . . . . . . . . . . . . . . . What do developers want? 419. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . What is the core of your API? 420. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using access levels properly 421. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Exploring your interface 424. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Language features 426. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Documenting your code 437. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Publishing to the world 444. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 448. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 449. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Conclusion 450. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Expert Swift raywenderlich.com 9
📄 Page
10
LBook License By purchasing Expert Swift, you have the following license: • You are allowed to use and/or modify the source code in Expert Swift in as many apps as you want, with no attribution required. • You are allowed to use and/or modify all art, images and designs that are included in Expert Swift in as many apps as you want, but must include this attribution line somewhere inside your app: “Artwork/images/designs: from Expert Swift, available at www.raywenderlich.com”. • The source code included in Expert Swift is for your personal use only. You are NOT allowed to distribute or sell the source code in Expert Swift without prior authorization. • This book is for your personal use only. You are NOT allowed to sell this book without prior authorization, or distribute it to friends, coworkers or students; they would need to purchase their own copies. All materials provided with this book are provided on an “as is” basis, without warranty of any kind, express or implied, including but not limited to the warranties of merchantability, fitness for a particular purpose and noninfringement. In no event shall the authors or copyright holders be liable for any claim, damages or other liability, whether in an action of contract, tort or otherwise, arising from, out of or in connection with the software or the use or other dealings in the software. All trademarks and registered trademarks appearing in this guide are the properties of their respective owners. raywenderlich.com 10
📄 Page
11
Before You Begin This section tells you a few things you need to know before you get started, such as what you’ll need for hardware and software, where to find the project files for this book, and more. raywenderlich.com 11
📄 Page
12
iWhat You Need To follow along with this book, you’ll need the following: • A Mac running macOS Catalina 10.15 or later, with the latest point release and security patches installed. This is so you can install the latest version of the required development tool: Xcode. • Xcode 12.5 or later. You can download the latest version of Xcode for free from the Mac App Store, here: apple.co/1FLn51R If you haven’t installed the latest version of Xcode, be sure to do that before continuing with the book. The code covered in this book depends on Swift 5.4 and Xcode 12.5 — you may get lost if you try to work with an older version or work outside the playground environment that this book assumes. raywenderlich.com 12
📄 Page
13
iiBook Source Code & Forums Where to download the materials for this book The materials for this book can be cloned or downloaded from the GitHub book materials repository: • https://github.com/raywenderlich/advs-materials/tree/editions/1.0 Forums We’ve also set up an official forum for the book at https://forums.raywenderlich.com/ c/books/expert-swift/78. This is a great place to ask questions about the book or to submit any errors you may find. raywenderlich.com 13
📄 Page
14
“Thanks to my family for their unconditional support, and my beautiful wife Merche for being a wonderful blessing.” — Ehab Amer “To my dad, for teaching me all of the things I thought were too hard to know.” — Marin Bencevic “To my friends and family and the Swift community of dreamers, inventors, teachers, learners and doers. You guys make this place awesome.” — Ray Fix “For my beautiful family: Elia and Baby Ethan. You’re everything to me: my love, inspiration, and rock ❤ . To my family and friends for their support, you’re the best!” — Shai Mishali Expert Swift Dedications raywenderlich.com 14
📄 Page
15
About the Authors Ehab Amer is an author of this book. He is a very enthusiastic Lead iOS developer with a very diverse experience from building games to enterprise applications and POCs with new technologies. In his spare time, TV shows take the majority of his time, followed by video games. When away from the screen, he goes with his wife and friends to explore the underwater world through diving. Marin Bencevic is a computer vision researcher working on medical images. He is also an iOS developer who likes to work on cool iOS apps and games, nerd out about programming, learn new things and then blog about it. He also has a chubby cat. Ray Fix works on next-generation microscopes made for iPad at Discover Echo Inc. in San Diego, California. Ray enjoys learning new things and is excited about math, data, visualization, machine learning and computer vision. Swift is his problem-solving language of choice and he has been using it and teaching others about it since its 2014 public release. Shai Mishali is an author on this book. He’s an experienced, award-winning iOS specialist; as well as an international speaker, and a highly active open-source contributor and maintainer on several high-profile projects - namely, the RxSwift Community and RxSwift projects, but also releases many open-source endeavors around Combine such as CombineCocoa, RxCombine and more. As an avid enthusiast of hackathons, Shai took 1st place at BattleHack Tel-Aviv 2014, BattleHack World Finals San Jose 2014, and Ford’s Developer Challenge Tel-Aviv 2015. You can find him on GitHub and Twitter as @freak4pc. Expert Swift Dedications raywenderlich.com 15
📄 Page
16
About the Editors Morten Faarkrog is the technical editor of this book. He is a Technical Director & Solution Architect at a consultancy in Denmark, where he works with architecting larger software solutions across a range of companies. Morten has a background as an iOS developer and still loves tinkering with the Swift language in his spare time. Daniel Souza comes from Cascavel-Ceara - a small city in Brazil.He started his career as a generalist and worked as a Backend, Frontend, and Android.But then he decided to focus on iOS Development and he couldn’t be happier. He loves the iOS Dev Community and how much you can learn from it. John Hagemann is the English language editor of this book. He is a communications specialist and analyst who enjoys putting technical subject matter into plain talk. He lives in Washington state with his son. Eli Ganim is the final pass editor for this book. He is an iOS engineer from Israel, who’s passionate about teaching, writing and sharing knowledge with others. About the Artist Vicki Wenderlich is the designer and artist of the cover of this book. She is Ray’s wife and business partner. She is a digital artist who creates illustrations, game art and a lot of other art or design work for the tutorials and books on raywenderlich.com. When she’s not making art, she loves hiking, a good glass of wine and attempting to create the perfect cheese plate. Expert Swift Dedications raywenderlich.com 16
📄 Page
17
vIntroduction Welcome to Expert Swift! The book for intermediate Swift developers looking to enhance their skills and understanding of the language. Swift is a powerful and wonderful language, used by many developers around the world to build iOS, macOS, tvOS and watchOS apps. If you’re reading this book, then you’ve probably built a few apps yourself. Most of the books from raywanderlich.com are “By Tutorials”, however this book is a little different. Since this is an advanced book, we didn’t build a tutorial to accompany each chapter. Instead, we go deep by covering low-level concepts as well as high-level abstractions, by using sample projects to illustrate how the advanced topic might be used in real life. You should expect some longer chapters. Trying to explain advanced topics, like the ones this book covers, is not an easy task. Acquiring this advanced knowledge is worth it though, and as you read through the book, you’ll find yourself wanting to jump to your existing apps and implement the techniques you learn. Take a deep breath and enjoy the ride! raywenderlich.com 17
📄 Page
18
How to read this book Each chapter of this book presents some theory on the topic at hand, along with Swift code to demonstrate the practical applications of what you’re learning. The chapters are independent, meaning you can read them in any order you’d like. The only exception to this are the Protocols (chapter 3) and Generics (chapter 4) chapters, which share one sample project. Some chapters provide a starter project, while in some you’ll use playgrounds to write code from scratch. This book is split into three main sections: Section I: Core Concepts The first section of this book covers the basic building blocks of the Swift language: The type system (enums, structs and classes), Protocols and Generics. We’ll start with a brief refresher of each topic and then jump right into the behind-the-scenes implementations. The content of this section will expose the inner workings of the type system, as well as get you intimately familiar with protocols and generics. Section II: Standard Library This section covers the base layer of writing Swift programs: Numerics, Ranges, Strings, Sequences, Collections, Codable and the less obvious, but very important topic - Unsafe. As you’d expect from an advanced book, we don’t only explain these topics, but also investigate how they’re built, how they’re represented, and how to use them effectively. Section III: Techniques The final section of this book covers advanced techniques to super-charge your Swift powers, and use all of what Swift has to offer. We’ll cover topics like Higher order functions, Functional reactive programming, Objective-C interoperability, using Instrumentation, and API design. Expert Swift Introduction raywenderlich.com 18
📄 Page
19
Section I: Core Concepts The first section of this book covers the basic building blocks of the Swift language: The type system (enums, structs and classes), Protocols and Generics. We’ll start with a brief refresher of each topic and then jump right into the behind-the-scenes implementations. The content of this section will expose the inner workings of the type system, as well as get you intimately familiar with protocols and generics. raywenderlich.com 19
📄 Page
20
1Chapter 1: Introduction By Ray Fix In 2010, Chris Lattner typed mkdir shiny on his laptop, and what would ultimately become the Swift language was born. Shiny started as a personal project he worked on during evenings and weekends. The project had many ambitious goals. Lattner has mentioned some of them in interviews and podcasts, including: • Adopting modern language features that allow new programming paradigms. • Using automatic memory management to avoid the overhead of garbage collection. • Defining as much of the language as possible in the library instead of the compiler. • Making the language defaults safe to avoid costly undefined behavior. • Making it easy to learn for beginners. But perhaps most of all, he wanted this new language to be “real”. Using it should feel like using a scripting language but be suitable for everything from app development to system-level programming. Half-jokingly, he has called this, “world domination”. raywenderlich.com 20
The above is a preview of the first 20 pages. Register to read the complete e-book.