logo资料库

Mastering Swift 5 Deep dive into the latest edition.pdf

第1页 / 共358页
第2页 / 共358页
第3页 / 共358页
第4页 / 共358页
第5页 / 共358页
第6页 / 共358页
第7页 / 共358页
第8页 / 共358页
资料共358页,剩余部分请下载后查看
Cover
Title Page
Copyright and Credits
About Packt
Contributors
Table of Contents
Preface
Chapter 1: Taking the First Steps with Swift
What is Swift?
Swift features
Playgrounds
Getting started with playgrounds
iOS, tvOS, and macOS playgrounds
Showing images in a playground
Creating and displaying graphs in playgrounds
What playgrounds are not
Swift language syntax
Comments
Semicolons
Parentheses
Curly brackets
An assignment operator does not return a value
Spaces are optional in conditional and assignment statements
Hello World
Summary
Chapter 2: Learning about Variables, Constants, Strings, and Operators
Constants and variables
Defining constants and variables
Type safety
Type inference
Explicit types
Numeric types
Integer types
Floating-point and Double values
The Boolean type
The String type
Tuples
Enumerations
Operators
Assignment operator
Comparison operators
Arithmetic operators
Remainder operator
Compound assignment operators
Closed range operator
Half open range operator
Ternary conditional operator
Logical NOT operator
Logical AND operator
Logical OR operator
Summary
Chapter 3: Optional Types
Introducing optionals
The need for optional types in Swift
Defining an optional
Using optionals
Forced unwrapping of an optional
Optional binding
Optional types with tuples
Optional chaining
The nil coalescing operator
Summary
Chapter 4: Using Swift Collections
Swift collection types
Mutability
Arrays
Creating and initializing arrays
Accessing the array element
Counting the elements of an array
Is the array empty?
Shuffling an array
Appending to an array
Inserting a value into an array
Replacing elements in an array
Removing elements from an array
Merging two arrays
Retrieving a subarray from an array
Making bulk changes to an array
Algorithms for arrays
Sort
Sorted
Filter
Map
Count
forEach
Iterating over an array
Dictionaries
Creating and initializing dictionaries
Accessing dictionary values
Counting the key or values in a dictionary
Is the dictionary empty?
Updating the value of a key
Adding a key-value pair
Removing a key-value pair
Set
Initializing a set
Inserting items into a set
Determining the number of items in a set
Checking whether a set contains an item
Iterating over a set
Removing items in a set
Set operations
Summary
Chapter 5: Control Flow
What have we learned so far?
Curly brackets
Parentheses
Control flow
Conditional statements
The if statement
Conditional code execution with the if...else statement
The guard statement
The for-in loop
Using the for-in loop
The while loop
Using the while loop
Using the repeat-while loop
The switch statement
Switch on tuples
Match on wildcard
Using case and where statements with conditional statements
Filtering with the where statement
Filtering with the for-case statement
Using the if-case statement
Control transfer statements
The continue statement
The break statement
The fallthrough statement
Summary
Chapter 6: Functions
Using a single parameter function
Using a multi-parameter function
Defining a parameter's default values
Returning multiple values from a function
Adding external parameter names
Using variadic parameters
Inout parameters
Omitting argument labels
Putting it all together
Summary
Chapter 7: Classes, Structures, and Protocols
What are classes and structures?
Similarities between classes and structures
Differences between classes and structures
Value versus reference types
Creating a class or structure
Properties
Stored properties
Computed properties
Property observers
Methods
Custom initializers
Internal and external parameter names
Failable initializers
Access controls
Inheritance
Overriding methods and properties
Overriding methods
Overriding properties
Preventing overrides
Protocols
Protocol syntax
Property requirements
Method requirements
Extensions
Optional chaining
Summary
Chapter 8: Using Protocols and Protocol Extensions
Protocols as types
Polymorphism with protocols
Type casting with protocols
Protocol extensions
Do I need to use protocols?
Swift's standard library
Summary
Chapter 9: Protocol Oriented Design
Requirements
Object-oriented design
Protocol-oriented design
Protocol inheritance
Protocol composition
Protocol-oriented design
Using the where statement with protocols
Structures versus classes
The array structure
Summary
Chapter 10: Generics
Introducing generics
Generic functions
Generic types
Conditionally adding extensions with generics
Conditional conformance
Generic subscripts
Associated types
Summary
Chapter 11: Availability and Error Handling
Native error handling
Representing errors
Throwing errors
Catching errors
The availability attribute
Summary
Chapter 12: Custom Subscripting
Introducing subscripts
Subscripts with Swift arrays
Creating and using custom subscripts
Read-only custom subscripts
Calculated subscripts
Subscript values
External names for subscripts
Multidimensional subscripts
Dynamic member lookup
When not to use a custom subscript
Summary
Chapter 13: Working with Closures
An introduction to closures
Simple closures
Shorthand syntax for closures
Using closures with Swift's array algorithms
Changing functionality
Selecting a closure based on results
Summary
Chapter 14: Concurrency and Parallelism in Swift
Concurrency and parallelism
Grand Central Dispatch
Calculation type
Creating queues
Creating and using a concurrent queue
Creating and using a serial queue
Async versus sync
Executing code on the main queue function
Using asyncAfter
Using the Operation and OperationQueue types
Using BlockOperation
Using the addOperation() method of the operation queue
Subclassing the Operation class
Summary
Chapter 15: Custom Types
Value types and reference types
Recursive data types for reference types
Inheritance for reference types
Dynamic dispatch
Copy-on-write
Implementing the equatable protocol
Summary
Chapter 16: Memory Management
How ARC works
Strong reference cycles
Summary
Chapter 17: Swift Formatting and Style Guider
What is a programming style guide?
Your style guide
Do not use semicolons at the end of statements
Do not use parentheses for conditional statements
Naming
Custom types
Functions and methods
Constants and variables
Indenting
Comments
Using the self keyword
Constants and variables
Optional types
Using optional binding
Using optional chaining instead of optional binding for multiple unwrapping
Using type inference
Using shorthand declaration for collections
Using switch rather than multiple if statements
Don't leave commented-out code in your application
Summary
Chapter 18: Adopting Design Patterns in Swift
What are design patterns?
Creational patterns
The singleton design pattern
Understanding the problem
Understanding the solution
Implementing the singleton pattern
The builder design pattern
Understanding the problem
Understanding the solution
Implementing the builder pattern
Structural design patterns
The bridge pattern
Understanding the problem
Understanding the solution
Implementing the bridge pattern
The facade pattern
Understanding the problem
Understanding the solution
Implementing the facade pattern
The proxy design pattern
Understanding the problem
Understanding the solution
Implementing the proxy pattern
Behavioral design patterns
The command design pattern
Understanding the problem
Understanding the solution
Implementing the command pattern
The strategy pattern
Understanding the problem
Understanding the solution
Implementing the strategy pattern
Summary
Other Books You May Enjoy
Index
Mastering Swift 5 Fifth Edition Deep dive into the latest edition of the Swift programming language Jon Hoffman BIRMINGHAM - MUMBAI
Mastering Swift 5 Fifth Edition Copyright © 2019 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. Commissioning Editor: Kunal Chaudhari Acquisition Editor: Larissa Pinto Content Development Editor: Arun Nadar Technical Editor: Jinesh Topiwala Copy Editor: Safis Editing Project Coordinator: Kinjal Bari Proofreader: Safis Editing Indexer: Pratik Shirodkar Graphics: Alishon Mendonsa Production Coordinator: Nilesh Mohite First published: June 2015 Second edition: November 2015 Third Edition: October 2016 Forth Edition: September 2017 Fifth Edition: April 2019 Production reference: 2241019 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78913-986-0 www.packt.com
Packt.com Subscribe to our online digital library for full access to over 7,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website. Why subscribe? Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals Improve your learning with Skill Plans built especially for you Get a free eBook or video every month Fully searchable for easy access to vital information Copy and paste, print, and bookmark content Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.packt.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at customercare@packtpub.com for more details. At www.packt.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks.
Contributors About the author Jon Hoffman has over 20 years' experience in the field of information technology. Over those 20 years, Jon has worked in the system administration, network administration, network security, application development, and architecture arenas. Currently, Jon works as an Enterprise Software Manager at Syntech Systems. Jon has developed extensively for the iOS platform since 2008. This includes several apps that he has published in the App Store, apps that he has written for third parties, and numerous enterprise applications. Some of Jon's other interests are watching Baseball (Go Sox) and Basketball (Go Celtics). Jon also really enjoys Tae Kwon Do, where he and his oldest daughter earned their black belts together early in 2014. With this being the fifth edition of the Mastering Swift book, I would like to thank everyone who has given me encouragement, positive feedback, and constructive criticism through the years. That includes my family, friends, co-workers, and everyone at Packt.
About the reviewers Gene De Lisa has been a software developer since the early 1980s in a wide variety of languages and platforms, He has been programming in iOS since 2009, and Swift since its release day in June 2014. He earned his doctorate in 1993. He has been an independent consultant in Enterprise Java since the 1990s and iOS since 2012. Vinod Madigeri is a senior software engineer with expertise in full-stack app development. He received his Master’s in Computer Science from the University of Utah in 2015. He has worked in several industries (telecommunication, game technologies, and consumer electronics) as a developer, team leader, and mentor writing software — in C, C++, Python, Objective-C, and Swift — for macOS and iOS platforms. Currently, he spends much of his time exploring Machine Learning as a skill to complement his software development efforts. Vinod has also been a technical reviewer for Object–Oriented Programming with Swift, Hands-On Full-Stack Development with Swift, and Multiplayer Game Development with HTML5. I'd like to thank my wife, Shruti, for her constant support, encouragement, and vanguard thoughts. Packt is searching for authors like you If you're interested in becoming an author for Packt, please visit authors.packtpub.com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea.
Table of Contents Preface Chapter 1: Taking the First Steps with Swift 1 Swift language syntax What is Swift? Swift features Playgrounds Getting started with playgrounds iOS, tvOS, and macOS playgrounds Showing images in a playground Creating and displaying graphs in playgrounds What playgrounds are not Comments Semicolons Parentheses Curly brackets An assignment operator does not return a value Spaces are optional in conditional and assignment statements 6 6 9 10 10 14 14 18 19 20 20 23 24 25 26 27 28 29 Chapter 2: Learning about Variables, Constants, Strings, and Operators 30 31 32 33 34 35 36 36 39 41 42 47 48 52 52 53 53 54 54 Assignment operator Comparison operators Arithmetic operators Remainder operator Compound assignment operators Hello World Summary Constants and variables Defining constants and variables Type safety Type inference Explicit types Numeric types Integer types Floating-point and Double values The Boolean type The String type Tuples Enumerations Operators
Table of Contents Closed range operator Half open range operator Ternary conditional operator Logical NOT operator Logical AND operator Logical OR operator Summary Chapter 3: Optional Types Introducing optionals The need for optional types in Swift Defining an optional Using optionals Forced unwrapping of an optional Optional binding Optional types with tuples Optional chaining The nil coalescing operator Summary Chapter 4: Using Swift Collections Swift collection types Arrays Mutability Creating and initializing arrays Accessing the array element Counting the elements of an array Is the array empty? Shuffling an array Appending to an array Inserting a value into an array Replacing elements in an array Removing elements from an array Merging two arrays Retrieving a subarray from an array Making bulk changes to an array Algorithms for arrays Sort Sorted Filter Map Count forEach Dictionaries Iterating over an array Creating and initializing dictionaries [ ii ] 55 55 56 56 56 57 57 58 58 60 61 61 61 62 64 64 64 65 66 66 67 67 68 69 70 71 72 72 73 73 73 74 74 75 76 76 77 77 78 79 79 80 81 81
分享到:
收藏