Atnaujinkite slapukų nuostatas

Head First Swift [Minkštas viršelis]

4.00/5 (12 ratings by Goodreads)
  • Formatas: Paperback / softback, 400 pages, aukštis x plotis x storis: 241x202x23 mm, weight: 858 g
  • Išleidimo metai: 30-Nov-2021
  • Leidėjas: O'Reilly Media
  • ISBN-10: 1491922850
  • ISBN-13: 9781491922859
  • Formatas: Paperback / softback, 400 pages, aukštis x plotis x storis: 241x202x23 mm, weight: 858 g
  • Išleidimo metai: 30-Nov-2021
  • Leidėjas: O'Reilly Media
  • ISBN-10: 1491922850
  • ISBN-13: 9781491922859
What will you learn from this book? Swift is best known as Apple's programming language of choice for developing apps on iOS, iPadOS, macOS, watchOS, and tvOS. But it's far more versatile than that. Open source Swift is also gaining ground as a language for systems programming and server-side code, and it runs on Linux and Windows. So where do you start? With Head First Swift, you'll explore from the ground up: from collecting and controlling data to reusing code, producing custom data types, and structuring programs and user interfaces with SwiftUI by building safe, protocol-driven code. With Swift under your belt, you'll be ready to build everything from mobile and web apps to games, frameworks, command-line tools, and beyond.

What's so special about this book?

If you've read a Head First book, you know what to expect--a visually rich format designed for the way your brain works. If you haven't, you're in for a treat. With this book, you'll learn Swift through a multisensory experience that engages your mind rather than a text-heavy approach that puts you to sleep.
Intro Your brain on Swift.
Here you are trying to learn something, while here your brain is doing you a favor by making sure the learning doesn't stick.
Your brain's thinking, "Better leave room for more important things, like which wild animals to avoid and whether naked snowboarding is a bad idea."
So how do you trick your brain into thinking that your life depends on knowing Swift?
Who is this book for?
xxii
We know what you're thinking
xxiii
We know what your brain is thinking
xxiii
Metacognition: thinking about thinking
xxv
Here's what we did
xxvi
Here's what you can do to bend your brain into submission
xxvii
Read me
xxviii
The technical review team
xxx
Acknowledgments
xxxi
1 Introducing Swift: Apps, Systems, and Beyond!
Swift is a programming language you can rely on.
It's a programming language you can take home to meet your family.
Safe, reliable, speedy, friendly, easy to talk to.
And while Swift is best known for being the programming language of choice for Apple's platforms, such as iOS, macOS, watchOS, and tvOS, the open source Swift project also runs on Linux and Windows, and is gaining ground as a systems programming language, as well as on the server.
You can build everything from mobile apps, to games, to web apps, to frameworks and beyond.
Let's get started!
Swift is a language for everything
2(2)
The swift evolution of Swift
4(1)
Swift into the future
5(1)
How you're going to write Swift
6(2)
The path you'll be taking
8(1)
Getting Playgrounds
9(2)
Creating a Playground
11(1)
Using a Playground to code Swift
12(2)
Basic building blocks
14(6)
A Swift example
20(5)
Congrats on your first steps with Swift!
25(5)
2 Swift by Name: Swift by Nature
You already know the gist of Swift.
But it's time to dive into the building blocks in more detail.
You've seen enough Swift to be dangerous, and it's time to put it into practice.
You'll use Playgrounds to write some code, using statements, expressions, variables, and constants-the fundamental building blocks of Swift.
In this chapter, you'll build the foundation for your future Swift programming career.
You'll get to grips with Swift's type system, and learn the basics of using strings to represent text.
Let's get going and you can see for yourself how swiftly you can write Swift code.
Building from the blocks
30(1)
Basic operators
31(1)
Operating swiftly with mathematics
32(1)
Expressing yourself
33(3)
Names and types: peas in a pod
36(4)
Not all data is numbers
40(2)
Stringing things along with types
42(6)
String interpolation
48(8)
3 Collecting and controlling: Going Loopy for Data
You already know about expressions, operators, variables, constants, and types in Swift.
It's time to consolidate and build on that knowledge and explore some more advanced Swift data structures and operators: collections and control flow.
In this chapter, we're going to talk about putting collections of data into variables and constants, and how to structure data, manipulate data, and operate on data using control flow statements.
We'll be looking at other ways to collect and structure data later in the book, but for now let's get started with arrays, sets, and dictionaries.
Sorting pizzas
56(1)
Swift collection types
57(1)
Collecting values in an array
58(2)
How big is that array, exactly? Is it empty?
60(1)
Collecting values in a set
61(2)
Collecting values in a dictionary
63(2)
Tuples
65(1)
Everyone needs a good alias
66(2)
Control flow statements
68(1)
if statements
69(1)
switch statements
70(4)
Building a switch statement
74(2)
Range operators
76(1)
More complex switch statements
77(1)
Getting repetitive with loops
78(1)
Building a for loop
79(3)
Building a while loop
82(1)
Building a repeat-while loop
83(1)
Solving the pizza-sorting problem
84(2)
Phew, that's a lot of Swift!
86(13)
4 Functions and Enums: Reusing Code on Demand
Functions in Swift allow you to package up a specific behavior or unit of work into a single block of code that you can call from other parts of your program.
Functions can be standalone, or they can be defined as part of a class, a structure, or an enumeration, where they are usually referred to as methods.
Functions let you break down complex tasks into smaller, more manageable, and more testable units.
They're a core part of the way you structure a program with Swift.
Functions in Swift let you reuse code
99(1)
Built-in functions
100(1)
What can we learn from built-in functions?
101(3)
Improving the situation with a function
104(1)
Writing the body of the function
105(1)
Using functions
106(2)
Functions deal in values
108(2)
Many happy returns (from your functions)
110(2)
A variable number of parameters
112(3)
What can you pass to a function?
115(1)
Every function has a type
116(4)
Function types as parameter types
120(2)
Multiple return types
122(2)
Functions don't have to stand alone
124(3)
Switching with enums
127(7)
5 Closures: Fancy, Flexible Functions
Functions are useful, but sometimes you need more flexibility.
Swift allows you to use a function as a type, just like an integer or string.
This means you can create a function and assign it to a variable.
Once it's assigned to a variable, you can call the function using the variable, or pass the function to other functions as a parameter.
When you create and use a function in this manner, it's called a closure.
Closures are useful because they can capture references to constants and variables from the context in which they are defined.
This is called closing over a value, hence the name.
Meet the humble closure
134(2)
Closures are better with parameters
136(8)
Boiling it all down to something useful
144(1)
Reducing with closures
145(3)
Capturing values from the enclosing scope
148(2)
Escaping closures: the contrived example
150(3)
Autoclosures provide flexibility
153(3)
Shorthand argument names
156(7)
6 Structures, properties, and methods: Custom Types and Beyond
Working with data often involves defining your own kinds of data.
Structures-often shortened to their Swift language keyword, structs-allow you to create your own custom data types (just like String and Int are data types) by combining other types.
Using structs to represent the data that your Swift code is working with allows you to step back and consider how the data that flows through your code fits together.
Structs can hold variables and constants (which are called properties when they're inside a struct) and functions (which are called methods).
Let's add some structure to your world, and dive into structs.
Let's make a pizza, in all its glory
163(6)
The initializer behaves just like a function
169(3)
Static properties make structures more flexible
172(4)
Methods inside structures
176(1)
Changing properties using methods
177(2)
Computed properties
179(4)
Getters and setters for computed properties
183(1)
Implementing a setter
184(2)
Swift Strings are actually structs
186(1)
The case for lazy properties
187(1)
Using lazy properties
188(6)
7 Classes, actors, and inheritance: Inheritance is Always a Thing
Structs showed us how useful it can be to build custom types.
But Swift has a number of other tricks up its sleeve, including classes.
Classes are similar to structs: they let you make new data types that have properties and methods within them.
However, in addition to being reference types-instances of a specific class share a single copy of their data (unlike structs, which are value types and get copied)-classes support inheritance.
Inheritance allows one class to build upon the features of another.
A struct by any other name (that name: a class)
194(2)
Inheritance and classes
196(4)
Overriding methods
200(4)
Final classes
204(4)
Automatic reference counting
208(1)
Mutability
209(13)
8 Protocols and extensions: A Swift Lesson in Protocol
You know all about classes and inheritance, but Swift has a few more tricks for structuring programs, and they're a lot Swiftier.
Meet protocols and extensions.
Protocols in Swift let you define a blueprint specifying methods and properties that are required for some purpose, or some piece of functionality.
A protocol is adopted by a class, structure, or enumeration, and the actual implementation happens there.
Types that provide the functionality needed, and adopt a protocol, are referred to as conforming to that protocol.
Extensions simply let you add new functionality to existing types.
The Robot Factory
222(5)
Protocol inheritance
227(2)
Mutating methods
229(3)
Protocol types and collections
232(4)
Computed properties in extensions
236(4)
Extending a protocol
240(1)
Useful protocols and you
241(4)
Conforming to Swift's protocols
245(10)
9 Optionals, unwrapping, generics, and more: Nothing Optional About It
Dealing with data that doesn't exist can be challenging.
Thankfully, Swift has a solution.
Meet optionals.
In Swift, an optional allows you to work with a value, or with the absence of a value.
They're one of the many ways that Swift is designed to be a safe language.
You've occasionally seen optionals in your code so far, and now we're going to explore them in more depth.
Optionals make Swift safe because they keep you from accidentally writing code that would break if it's missing data, or if something can sometimes return a value that isn't actually a value.
Dealing with something that's missing
255(1)
Why you might need an optional
256(3)
Optionals and handling missing data
259(1)
Unwrapping optionals
260(3)
Unwrapping optionals with guard
263(1)
Force unwrapping
264(10)
Generics
274(2)
Here's our new Queue type
276(6)
10 Getting started with SwiftUI: User Interfaces...Swiftly
It's time to use your toolbox full of Swift techniques, features, and components: you're going to start building user interfaces.
A Swift UI, if you will.
We're going to bring everything together in this chapter to create our first true user interface.
We'll build a whole experience using SwiftUI, the user interface framework for Apple's platforms.
We'll still be using Playgrounds, at least initially, but everything we're doing here will lay the groundwork for an actual iOS application.
Get ready: this chapter is full of code and a lot of new concepts.
What's a UI framework, anyway?
282(2)
Your first SwiftUI
284(4)
UI building blocks
288(1)
Making a list, checking it...quite a few times, to get it perfect
289(2)
User interfaces with state
291(1)
Buttons are for pressing
292(3)
Let's see how far you've come
295(2)
Customizing your views with modifiers
297(3)
Create a new SwiftUI Xcode project, for iOS
300(2)
Your Xcode will look something like this
302(1)
Create a new type to store a todo item in
303(3)
Make sure each todo item can be uniquely identified
306(1)
Create a user interface for the app
307(7)
Implement a way to save the list of todos
314(5)
So, that's a UI framework?
319(9)
11 Putting SwiftUI into Practice: Circles, Timers, Buttons-Oh, My!
SwiftUI lets you do more than work with Buttons and Lists.
You can also use shapes, animations, and more! In this chapter, we're going to look at some of the more advanced ways you can construct a SwiftUI UI, and connect it to a data source that isn't just user-generated content like todo items.
SwiftUI lets you build responsive UIs that handle events coming from all sorts of places.
We'll be working with Xcode, Apple's IDE, and we'll be focusing on iOS apps, but everything you'll learn is applicable to SwiftUI on iPadOS, macOS, watchOS, and tvOS as well.
It's time to explore the depths of SwiftUI!
What fancy things can be done with a UI framework?
328(3)
Create a new SwiftUI Xcode project, for iOS
331(2)
The Executive Timer UI and features
333(1)
Creating the basic elements of the app
334(1)
Pieces of the UI
335(1)
Setting up the UI of the Executive Timer
336(1)
Coding the pieces of the UI
337(3)
Combining the three elements
340(2)
The finishing touches
342(2)
Tabbed views for a clean UI
344(2)
Build a TabView containing your views
346(4)
Creating a new tabbed ContentView
350(1)
Creating the tabs and the Tab View
351(2)
Running your new tabbed Executive Timer
353(5)
12 Apps, Web, and beyond: Putting It All Together
You've done lots of Swift. You've used Playgrounds and Xcode.
We knew you'd have to say goodbye eventually, but the time has finally arrived.
It's hard to part, but we know you've got what it takes.
In this chapter, our final chapter together (at least in this book), we're going to do a final pass through many of the concepts you've learned, and build a handful of things together.
We'll make sure your Swift is shipshape, and give you some pointers on what to do next.
Some homework, if you will.
It'll be fun, and we'll part on a high note.
A journey must end
358(5)
A recipe for a welcome screen
363(1)
Step by step assembly of the welcome screen
364(6)
Share the state
370(1)
It's time for our old friend
371(2)
Building an app with multiple views that share state
373(1)
Building a two-view score tracker
374(1)
The ObservableObject
374(1)
The first view
375(1)
The second view
376(1)
The first view, again
377(6)
A fancy AsyncImage
383(4)
Meet Vapor, the Swift web framework
387(3)
Sending data over the web with Vapor
390
Jon Manning is a world renown iOS development trainer and writer, game designer and mobile software engineering wizard.

He has co-authored two books on mobile development and enjoys re-implementing such things as OpenGL and the Objective-C runtime (in his spare time).

Jon is also a researcher in Human-Computer Interaction working towards a PhD.

Paris Buttfield-Addison is a mobile app engineer, game designer, and researcher with a passion for making technology simpler and as engaging as possible. Paris recently submitted a PhD investigating how people use tablets for information management.