Swift 2 By Example
上QQ阅读APP看书,第一时间看更新

Preface

The introduction of Swift during WWDC 2014 surprised the whole community of iOS developers, who were waiting to be introduced to the new API from iOS 8, not to be transformed into beginners.

Besides the surprise, most of them understood that this would have been a great opportunity to create a new world of libraries, patterns, best practices, and so on. On the other hand, communities of programmers in different languages, who were intimidated by the rough first impact of Objective-C, started to be attracted by Swift, which—with its friendly syntax—was less intimidating.

After a year, Swift 2.0 has proven to be a huge improvement over the first version, enriched by a lot of new features and strengthened by its use in thousands of new apps. It is finally ready for production!

In WWDC 2015, Apple made another surprising announcement: Swift will be open source and there will be versions for different operating systems, beginning with Linux and Windows. This opens up a whole new scenario, where it will be possible to implement both client and the server apps with the same language.

This book will introduce you the world of app development using the new features in Swift 2, and it will show you how to build Linux apps in Swift in order to create server counterparts of your mobile apps.

Through simple step-by-step chapters, the book will teach you how to build both utility and game apps; while building them, you'll learn the basics of Swift and iOS.

What this book covers

Chapter 1, Welcome to the World of Swift, introduces the Swift syntax and the most important features provided by the language.

Chapter 2, Building a Guess the Number App, introduces Xcode, its project file, and the different editors required to build an app; a simple game app will be created to demonstrate these.

Chapter 3, A Memory Game in Swift, shows the creation of a complete game app, with images and animations, without using any Game framework but with only the fundamental iOS libraries.

Chapter 4, A TodoList App in Swift, teaches you how to create a real-world utility app, handling the library dependencies with Cocoapods.

Chapter 5, A Pretty Weather App, shows you how to create a nice looking app that retrieves data from third-party servers.

Chapter 6, Flappy Swift, covers SpriteKit, the 2D iOS game engine.

Chapter 7, Polishing Flappy Swift, completes the game, adding Game Center support and various entertaining touches.

Chapter 8, Cube Runner, covers SceneKit and 3D programming, implementing a 3D endless runner with a space theme.

Chapter 9, Completing Cube Runner, finalizes the game and adds Game Center support.

Chapter 10, ASAP – an E-commerce App in Swift, implements an ecommerce app that uses local storage to store the products.

Chapter 11, ASAPServer, a Server in Swift, is a follow-up to the previous chapter, where we'll implement a backend server for Linux to handle the requests of the ASAP e-commerce app.

What you need for this book

In order to get the most out of this book, there are a few essentials you will need:

  • A Mac computer running OS X 10.11.2 or higher
  • A basic knowledge of programming
  • Xcode 7.2 or higher
  • An iPhone 5s or higher (an app uses CoreMotion that doesn't work in the simulator)

Who this book is for

This book is ideal for those who want to learn how to develop apps in Swift, starting the right way. Whether you are an expert Objective-C programmer or are new to this platform, you'll quickly grasp the code for real-world apps and discover how to use Swift effectively. Prior experience in the development of Apple devices would be helpful, but it is not mandatory.

Conventions

In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "We can include other contexts through the use of the include directive."

A block of code is set as follows:

let mainWindow = UIWindow(frame: UIScreen.mainScreen().bounds)
mainWindow.backgroundColor = UIColor.whiteColor()
mainWindow.rootViewController = navigatorViewController
mainWindow.makeKeyAndVisible()
window = mainWindow
return true

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

let mainWindow = UIWindow(frame: UIScreen.mainScreen().bounds)
mainWindow.backgroundColor = UIColor.whiteColor()
mainWindow.rootViewController = navigatorViewController
mainWindow.makeKeyAndVisible()
window = mainWindow
return true

New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "Clicking the Next button moves you to the next screen."

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book, what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail , and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for this book from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

You can download the code files by following these steps:

  1. Log in or register to our website using your e-mail address and password.
  2. Hover the mouse pointer on the SUPPORT tab at the top.
  3. Click on Code Downloads & Errata.
  4. Enter the name of the book in the Search box.
  5. Select the book for which you're looking to download the code files.
  6. Choose from the drop-down menu where you purchased this book from.
  7. Click on Code Download.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR / 7-Zip for Windows
  • Zipeg / iZip / UnRarX for Mac
  • 7-Zip / PeaZip for Linux

Downloading the color images of this book

We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from http://www.packtpub.com/sites/default/files/downloads/Swift2byExample_ColorImages.pdf.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at , and we will do our best to address the problem.