32,99 €
Transition from Objective-C to the cleaner, more functional Swift quickly and easily Professional Swift shows you how to create Mac and iPhone applications using Apple's new programming language. This code-intensive, practical guide walks you through Swift best practices as you learn the language, build an application, and refine it using advanced concepts and techniques. Organized for easy navigation, this book can be read end-to-end for a self-paced tutorial, or used as an on-demand desk reference as unfamiliar situations arise. The first section of the book guides you through the basics of Swift programming, with clear instruction on everything from writing code to storing data, and Section II adds advanced data types, advanced debugging, extending classes, and more. You'll learn everything you need to know to make the transition from Objective-C to Swift smooth and painless, so you can begin building faster, more secure apps than ever before. * Get acquainted with the Swift language and syntax * Write, deploy, and debug Swift programs * Store data and interface with web services * Master advanced usage, and bridge Swift and Objective-C Professional Swift is your guide to the future of OS X and iOS development.
Sie lesen das E-Book in den Legimi-Apps auf:
Seitenzahl: 427
Veröffentlichungsjahr: 2015
THE POPULARITY OF IOS has drawn many programmers to Apple’s platforms. Software on both OS X and iOS has long been written in Objective-C, a language first developed in the 1980s that is best described as an amalgamation of C and Smalltalk. Objective-C lacks many features that modern programmers expect to find in a programming language. Over the last ten years, Apple has adapted and improved Objective-C, but many programmers still find it to be an alien language.
Enter Swift. Swift is a modern programming language, built with knowledge gained in the last 30 years of programming language research. Developed by the same researchers who developed clang, the modern compiler toolchain used by Apple to build OS X and iOS software, Swift incorporates many features that are expected of a modern programming language. It sports a strong type system to prevent many of the mistakes prevalent in both C and Objective-C programming. It has a refined class system complete with static and computable properties. It supports closures and treats functions as first-class objects, allowing them to easily be saved as variables or passed to functions and methods as parameters. As you read this book, you’ll find many more advanced features that are present in Swift as well.
Swift has been designed from the ground up to be a modern programming language suitable for both systems and application development on Apple’s platforms. Apple has already thrown a lot of support behind Swift, and the language is constantly being improved. Swift represents the future of software development on iOS and OS X, and programmers working on that platform should expect Swift to occupy an increasingly dominant position on those systems.
This book is aimed at software developers with experience writing Objective-C applications on OS X or iOS. Some knowledge and experience with Swift are recommended but are by no means necessary to understand the material presented in this book. The book introduces the key concepts of Swift that may be new even to advanced iOS and OS X programmers. Over the course of the book, several small projects are used to illustrate the concepts as they are introduced. The text also makes extensive use of playgrounds, a new feature of Xcode that allows you to write and interact with Swift code without the overhead of an entire Xcode project.
The first chapter is an overview of Swift’s syntax and semantics but does not cover any advanced features of the language. The second chapter covers the use of Xcode, including playgrounds. If you already have some familiarity with Swift and Xcode, you may want to skip the first and second chapters and get started immediately with Chapter 3.
This book first offers a primer to the Swift programming language, released by Apple in June 2014. It quickly moves on to more advanced Swift programming topics, including the new playgrounds feature in Xcode; classes, structs, and enums; concurrent programming; and advanced features of Swift’s type system. It also covers practical topics that you will deal with on a regular basis as a Swift programmer, including interfacing with web services using JSON, Core Data, and using C and Objective-C code in your Swift project. It shows some of the advanced features of Xcode, including the use of the debugger built in to Xcode, and discusses the lower-level details of the Swift and Objective-C runtimes. And because it is impossible to completely avoid C when writing software on OS X and iOS, an index provides a quick overview of the C programming language.
Swift has been constantly evolving since its release in June 2014. This book covers version 1.1.
Because Swift is still changing, you may find some minor differences between the version discussed in this book and the latest released version. However, the vast majority of material presented in this book will still be valid for future versions of Swift.
This book is divided into two sections. The first section provides an introduction to the Swift programming language and covers many of the more practical topics you will encounter as a Swift programmer. The second section is a “deep dive” into the intricacies of Swift.
➤
Chapter 1:
Reviews the syntax and semantics of the Swift programming language. It is useful for those programmers who are new to the language or want a quick refresher.
➤
Chapter 2:
Covers the Swift-related changes to Xcode. It introduces Xcode’s new feature, playgrounds, which provide a way to interactively work with Swift code.
➤
Chapter 3:
Covers Swift’s classes, structs, and enums in greater detail than Chapter 1, providing a solid foundation for object-oriented programming in Swift.
➤
Chapter 4:
Discusses concurrent programming in the context of Swift and the Foundation framework on iOS and OS X.
➤
Chapter 5:
Shows you how to communicate with remote web services using JSON.
➤
Chapter 6:
Discusses how to use Core Data to store and search for information used by your program.
➤
Chapter 7:
Shows you how to extend classes using protocols and class extensions.
➤
Chapter 8:
Covers Swift’s type system in fine detail and shows you how to take advantage of Swift’s strong type system to write less error-prone code.
➤
Chapter 9:
Shows you how you can mix C and Objective-C code in your Swift programs.
➤
Chapter 10:
Introduces the debugger and shows you how you can track down and fix problems in your code.
➤
Chapter 11:
Discusses the runtime architecture of both Swift and Objective-C and demonstrates how programs are loaded and executed on iOS and OS X.
➤
Appendix:
Covers the features and concepts that the C programming language introduces.
If you are already familiar with Swift and Xcode, you may want to skip Chapters 1 and 2 and start right in on Chapter 3.
To run the samples in the book, you will need the following:
➤ A Mac running OS X 10.9 or later
➤ Xcode 6
➤ An iPhone, iPod Touch, or iPad if you want to run the examples on an actual device instead of the iOS simulator that ships with Xcode
The source code for the samples is available for download from the Wrox website at: www.wrox.com/go/proswift.
To help you get the most from the text and keep track of what’s happening, this book uses a number of conventions.
WARNING Warnings hold important, not-to-be-forgotten information that is directly relevant to the surrounding text.
NOTE Notes indicate notes, tips, hints, tricks, or asides to the current discussion.
As for styles in the text:
➤ We
highlight
new terms and important words when we introduce them.
➤ We show keyboard strokes like this: Ctrl+A.
➤ We show file names, URLs, and code within the text like so:
persistence.properties
.
We present code in two different ways:
We use a monofont type with no highlighting for most code examples.
We use bold to emphasize code that is particularly important in the present context or to show changes from a previous code snippet.
As you work through the examples in this book, you may choose either to type in all the code manually or to use the source code files that accompany the book. All the source code used in this book is available for download at www.wrox.com. Specifically for this book, the code download is on the Download Code tab at www.wrox.com/go/proswift.
You can also search for the book at www.wrox.com by ISBN (the ISBN for this book is 978-1-119-01677-9) to find the code. And a complete list of code downloads for all current Wrox books is available at www.wrox.com/dynamic/books/download.aspx.
At the beginning of each chapter, we’ve provided a list of the major code files for the chapter. Throughout each chapter, you’ll also find references to the names of code files as needed in listing titles and text.
Most of the code on www.wrox.com is compressed in a .ZIP, .RAR, or similar archive format appropriate to the platform. Once you download the code, just decompress it with an appropriate compression tool.
NOTE Because many books have similar titles, you may find it easiest to search by ISBN; this book’s ISBN is 978-1-119-01677-9.
Once you download the code, just decompress it with your favorite compression tool. Alternately, you can go to the main Wrox code download page at www.wrox.com/dynamic/books/download .aspx to see the code available for this book and all other Wrox books.
We make every effort to ensure that there are no errors in the text or in the code. However, no one is perfect, and mistakes do occur. If you find an error in one of our books, such as a spelling mistake or faulty piece of code, we would be very grateful for your feedback. By sending in errata, you may save another reader hours of frustration, and at the same time, you will be helping us provide even higher quality information.
To find the errata page for this book, go to www.wrox.com/go/proswift, and click the Errata link. On this page you can view all errata that has been submitted for this book and posted by Wrox editors.
If you don’t spot “your” error on the Book Errata page, go to www.wrox.com/contact/techsupport.shtml and complete the form there to send us the error you have found. We’ll check the information and, if appropriate, post a message to the book’s errata page and fix the problem in subsequent editions of the book.
For author and peer discussion, join the P2P forums at http://p2p.wrox.com. The forums are a Web-based system to which you can post messages relating to Wrox books and related technologies and where you can interact with other readers and technology users. The forums offer a subscription feature to e-mail you topics of interest of your choosing when new posts are made to the forums. Wrox authors, editors, other industry experts, and your fellow readers are present on these forums.
At http://p2p.wrox.com, you will find a number of different forums that will help you, not only as you read this book, but also as you develop your own applications. To join the forums, just follow these steps:
Go to
http://p2p.wrox.com
and click the Register link.
Read the terms of use and click Agree.
Complete the required information to join, as well as any optional information you wish to provide, and click Submit.
You will receive an e-mail with information describing how to verify your account and complete the joining process.
NOTE You can read messages in the forums without joining P2P, but in order to post your own messages, you must join.
Once you join, you can post new messages and respond to messages other users post. You can read messages at any time on the Web. If you would like to have new messages from a particular forum e-mailed to you, click the Subscribe to this Forum icon by the forum name in the forum listing.
For more information about how to use the Wrox P2P, be sure to read the P2P FAQs for answers to questions about how the forum software works, as well as many common questions specific to P2P and Wrox books. To read the FAQs, click the FAQ link on any P2P page.
Introduction
WHO THIS BOOK IS FOR
WHAT THIS BOOK COVERS
HOW THIS BOOK IS STRUCTURED
WHAT YOU NEED TO USE THIS BOOK
CONVENTIONS
SOURCE CODE
ERRATA
P2P.WROX.COM
Part I: Building Applications with Swift
1: A Swift Primer
WHAT IS SWIFT?
WHY LEARN SWIFT?
WORKING WITH CONSTANTS AND VARIABLES
WORKING WITH OPERATORS
MAKING DECISIONS WITH CONTROL FLOW
GROUPING TYPES WITH ENUMERATIONS
WORKING WITH FUNCTIONS
SUMMARY
2: Writing a Swift Program
SETTING UP XCODE
EXPERIMENTING WITH PLAYGROUNDS
WRITING SWIFT IN XCODE
DEBUGGING SWIFT APPLICATIONS
SUMMARY
3: Classes, Structs, and Enums
WORKING WITH CLASSES AND STRUCTS
WORKING WITH PROPERTIES
UNDERSTANDING METHODS
UNDERSTANDING THE DIFFERENCE BETWEEN CLASSES AND STRUCTS
WORKING WITH ENUMERATIONS
SUMMARY
4: Concurrency in Swift
WHAT IS CONCURRENCY?
CONCURRENCY IN iOS AND OS X
SUMMARY
5: Interfacing with Web Services
UNDERSTANDING WEB SERVICES
IMPLEMENTING A WEB SERVICES CLIENT
SUMMARY
6: Storing Data with Core Data
WHAT IS CORE DATA?
USING CORE DATA WITH SWIFT
SUMMARY
Part II: Advanced Swift Concepts
7: Extending Classes
WORKING WITH CLASS EXTENSIONS
SPECIFYING BEHAVIOR WITH PROTOCOLS
WORKING WITH GENERICS
SUMMARY
8: Advanced Data Types
WORKING WITH ENUMS AND ALGEBRAIC DATA TYPES
WORKING WITH OPTIONAL TYPES
UNDERSTANDING TYPE CASTING
GROUPING VALUES WITH TUPLES
CUSTOM OPERATORS
USING FUNCTIONS AND CLOSURES
SUMMARY
9: Bridging Swift and Objective-C
THE SUCCESSOR TO OBJECTIVE-C
INTRODUCING NAMESPACES AND MODULES
HOW SWIFT AND OBJECTIVE-C INTERACT
USING C AND C++ CODE WITH SWIFT
SUMMARY
10: Debugging Swift Applications
THE ART OF DEBUGGING
CREATING CIRCLEVIEW
PRINTING VALUES
WORKING WITH DEBUGGERS
EXAMINING ERRORS WITH LLDB, THE LLVM DEBUGGER
SUMMARY
11: The Swift Runtime
WHAT IS A RUNTIME?
UNDERSTANDING THE OBJECTIVE-C RUNTIME
EXPLORING THE SWIFT RUNTIME
SUMMARY
Appendix: An Overview of C
COMPARING PROCEDURAL AND OBJECT-ORIENTED PROGRAMMING STYLES
UNDERSTANDING THE IMPORTANCE OF C LANGUAGE SYNTAX
Title Page
Copyright
Dedication
About the Author
Credits
Acknowledgments
Advert
EULA
Chapter 8
Table 8.1
Chapter 9
Table 9.1
Table 9.2
Chapter 2
Figure 2.1
Figure 2.2
Figure 2.3
Figure 2.4
Figure 2.5
Figure 2.6
Figure 2.7
Figure 2.8
Figure 2.9
Figure 2.10
Figure 2.11
Figure 2.12
Figure 2.13
Figure 2.14
Figure 2.15
Figure 2.16
Figure 2.17
Figure 2.18
Figure 2.19
Figure 2.20
Figure 2.21
Figure 2.22
Figure 2.23
Figure 2.24
Chapter 5
Figure 5.1
Figure 5.2
Figure 5.3
Figure 5.4
Figure 5.5
Figure 5.6
Figure 5.7
Figure 5.8
Figure 5.9
Figure 5.10
Figure 5.11
Figure 5.12
Figure 5.13
Figure 5.14
Chapter 6
Figure 6.1
Figure 6.2
Figure 6.3
Figure 6.4
Figure 6.5
Figure 6.6
Figure 6.7
Figure 6.8
Figure 6.9
Figure 6.10
Chapter 9
Figure 9.1
Figure 9.2
Figure 9.3
Figure 9.4
Chapter 10
Figure 10.1
Figure 10.2
Figure 10.3
Figure 10.4
Figure 10.5
Figure 10.6
Figure 10.7
Figure 10.8
Figure 10.9
Figure 10.10
Figure 10.11
Figure 10.12
Figure 10.13
Figure 10.14
Figure 10.15
Cover
Table of Contents
Part I
xxiii
xxiv
xxv
xxvi
xxvii
1
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
48
49
50
51
52
53
54
55
56
57
58
59
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
103
104
105
106
107
109
110
111
112
114
115
116
118
119
120
121
122
123
124
125
126
128
129
132
133
134
135
136
137
138
139
140
145
147
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
▶
Chapter 1:
A Swift Primer
▶
Chapter 2:
Writing a Swift Program
▶
Chapter 3:
Classes, Structs, and Enums
▶
Chapter 4:
Concurrency in Swift
▶
Chapter 5:
Interfacing with Web Services
▶
Chapter 6:
Storing Data with Core Data
WHAT’S IN THIS CHAPTER?
➤ Understanding Swift
➤ Declaring constants and variables and working with Swift’s data types
➤ Transforming values with operators
➤ Controlling code execution with conditional statements and loops
➤ Defining and using Swift’s enumerated data types
➤ Understanding, declaring, and using functions, anonymous functions, and closures
This chapter introduces the key concepts featured in the Swift programming language and covers the language’s new syntax and data types. It is not intended as an introductory guide to Swift, but rather, as a way for programmers who have already worked with Swift to refresh their knowledge of the language. Prior knowledge of C and Objective-C programming on iOS and OS X is also assumed, although the information presented in this chapter should still make sense to those with no experience in C and Objective-C.
If you are already familiar with the foundations of the Swift programming language, you may want to dive right in with Chapter 2. Chapter 2 also shows you how to use Xcode’s new playgrounds feature, which allows you to experiment with the effects of Swift code as you write it. The examples in this chapter can be entered directly into a playground so you can see the results of your code immediately, without having to create an Xcode project and compile the code.
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!