ebook img

Transitioning to Swift PDF

228 Pages·2014·2.744 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Transitioning to Swift

Companion BOOKS FOR PROFESSIONALS BY PROFESSIONALS® eBook Available S C o w v i e f r t s S Developing apps for Apple’s broadening platform of devices 1 TLE is an exciting topic these days. Apple created the Swift Get up to speed on Swift quickly by leveraging .1 TI D programming language to build state-of-the-art apps using the ATE latest Apple technologies. your knowledge of Objective-C L E R In this 200-page book, author Scott Gardner articulates the similarities and differences between traditional Objective-C based programming and Swift, revealing what you need to know from syntax changes to emerging best practices and paradigm shifts, to write powerful, expressive, and flexible code in Swift. Written at a brisk pace and in a methodical style, you’ll learn T how to apply your Objective-C skills to successfully transition to r a programming in Swift. n In this book, you’ll learn: s • i What is Swift and how does it compare to Objective-C t • H ow to become proficient in Swift by leveraging your io existing Objective-C skills n • How to take advantage of new capabilities in Swift i • n What are the emerging best practices in Swift g programming t Transitioning to Swift reaches out to all developers who are o interested in creating state-of-the-art apps for Apple’s broadening S platform of devices for both consumers and enterprise. Apple’s w introduction of the new Swift programming language raises Transitioning to many questions. This book addresses those questions directly, and i f prepares developers for building the next generation of apps in t Swift to surprise and delight users the world over. Swift G a r d n Scott Gardner e COMPANION eBOOK r ISBN 978-1-4842-0407-8 US $29.99 52999 Shelve in Mobile Computing SOURCE CODE ONLINE User level: www.apress.com Beginning 9781484204078 www.it-ebooks.info For your convenience Apress has placed some of the front matter material after the index. Please use the Bookmarks and Contents at a Glance links to access them. www.it-ebooks.info Contents at a Glance About the Author ����������������������������������������������������������������������������xiii About the Technical Reviewer ���������������������������������������������������������xv Acknowledgments �������������������������������������������������������������������������xvii Who This Book Is For ����������������������������������������������������������������������xix ■ Chapter 1: Getting Started ��������������������������������������������������������������1 ■ Chapter 2: Declaring Variables and Constants �����������������������������13 ■ Chapter 3: Working with Strings and Collections �������������������������27 ■ Chapter 4: Performing Operations ������������������������������������������������49 ■ Chapter 5: Controlling Program Flow �������������������������������������������65 ■ Chapter 6: Creating Functions ������������������������������������������������������79 ■ Chapter 7: Constructing Classes, Structures, and Enumerations ����������������������������������������������������������������������105 ■ Chapter 8: Defining and Adopting Protocols ������������������������������151 v www.it-ebooks.info vi Contents at a Glance ■ Chapter 9: Subclassing and Extending ���������������������������������������167 ■ Chapter 10: Controlling Access ��������������������������������������������������185 ■ Chapter 11: Generic Programming ���������������������������������������������203 Index ����������������������������������������������������������������������������������������������213 www.it-ebooks.info 1 Chapter Getting Started In this chapter you will download, install, and set up the Apple developer tools necessary to follow along with this book. You will be introduced to the Swift programming language, by way of writing the venerable “Hello world” program and by seeing how two common actions are performed in Swift as compared with Objective-C: logging and commenting. Installing Xcode The minimum version of Xcode that supports writing Swift code is Xcode 6. Xcode 6 requires a Mac running OS X 10.9.3 or higher. The easiest way to install Xcode is via the App Store. From the menu select ➤ App Store.... Search for “xcode,” which should return Xcode as the top search result. Click the FREE button, which will change its label to INSTALL APP, and click that button again to start the download and install process, as shown in Figure 1-1. 1 www.it-ebooks.info 2 CHAPTER 1: Getting Started Figure 1-1. Drag to install Xcode in your Applications folder The INSTALL APP button label will change to INSTALLING while the app downloads and is installed. Weighing it at nearly 2.5 GB in size, this may take a while to download, and the only indication given within the App Store app is the button label. One way to observe the process is via the Launchpad app, which can be launched from /Applications folder if there is not a shortcut available on your Dock; Figure 1-2 demonstrates. Figure 1-2. Observing the download progress via Launchpad www.it-ebooks.info CHAPTER 1: Getting Started 3 Once installation is complete, a sparkle animation will appear on top of the Xcode app icon in Launchpad and the INSTALLING label in App Store will change to INSTALLED. Either click the Xcode app icon in Launchpad or locate and double-click the Xcode app icon in your /Applications folder to launch Xcode. An Xcode and iOS SDK License Agreement window will appear as shown in Figure 1-3. Review the terms and click Agree to proceed in launching Xcode. Figure 1-3. Xcode and iOS SDK License Agreement You will be asked to enter admin credentials in order for Xcode to install necessary components to your system. After entering admin credentials, a progress window will briefly appear during that installation, and then the Welcome to Xcode window should appear (see Figure 1-4). If not, select from the menu Window ➤ Welcome to Xcode. Creating a Playground In the Welcome to Xcode window, click Get started with a playground, or select File ➤ New ➤ Playground... from the menu. Figure 1-4. Welcome to Xcode www.it-ebooks.info 4 CHAPTER 1: Getting Started Accept or change the suggested filename, leave the platform selection as iOS, and then click Next (Figure 1-5) and save the file to a convenient location such as your ~/Documents folder. You may find it useful to also drag this file to your Dock to create a shortcut. Figure 1-5. Creating a playground Click Enable in the Enable Developer Mode on this Mac? window that appears, and again enter admin credentials when prompted. Your Swift playground file will appear (Figure 1-6), complete with a comment, import statement, and declaration of a string variable (more on that later). Figure 1-6. New playground Notice the import UIKit line, but there is no import Swift line, as there would similarly need to be an import Foundation line (or some other import that imports Foundation) in an Objective-C source file. This is because the Swift standard library is automatically imported. You could, in fact, delete the import UIKit line in your playground and the existing code would still run. www.it-ebooks.info CHAPTER 1: Getting Started 5 Also notice the "Hello, playground" printout on the right, which is the results sidebar. Swift playgrounds provide an interactive environment in which you can type Swift code and immediately see the results—no Xcode project and no build and run process required. Type the following code in the line below the variable declaration: println("Hello world") In addition to the results sidebar, you have the option displaying console output in the Assistant Editor. I have found that displaying the Assistant Editor on the bottom makes best use of screen real estate. To specify the location of the Assistant Editor, such as on the bottom, select View ➤ Assistant Editor ➤ Assistant Editors on Bottom. To actually display the Assistant Editor, select View ➤ Assistant Editor ➤ Show Assistant Editor (Figure 1-7). Figure 1-7. Playground with Assistant Editor Voilà! You now have a single-file, interactive Swift coding environment in which to write and observe the results of your Swift code. I’ve only scratched the surface of the power and versatility of Swift playgrounds, but it’s all you need to know for this book. I encourage you to watch the Swift Playgrounds WWDC video at the following URL for a deeper dive into the capabilities of playgrounds: https://developer.apple.com/videos/wwdc/2014/?id=408 www.it-ebooks.info 6 CHAPTER 1: Getting Started Running a REPL You may also set up and run a REPL — read, eval, print, loop — in order to write interactive Swift code in the command line. To enable this capability, open the Terminal app from your /Applications/Utilities folder and type xcrun swift (or lldb --repl) at the command prompt and press return. You will be welcomed to the Swift REPL (Figure 1-8). Type println("Hello world") at the 1> prompt and hit return, which will instruct the REPL to execute this function and print out, “Hello world.” Type :quit (or even just :q) and press return to exit out of the Swift REPL and return to the command line. Figure 1-8. Swift REPL Of course, you can also create an Xcode Swift project in the same traditional manner as you would create an Objective-C project in order to write, build, and run test/exploratory code. Logging to the Console Objective-C utilizes NSLog() to log messages to the console during runtime. NSLog() prefixes the provided string with a timestamp and the process ID, and adds a hard return to the end of the string. NSLog()’s closest counterpart in Swift is println() (print line). println() writes the provided string followed by a newline character. However, println() does not include a timestamp or process ID. Swift simplifies string interpolation in println(). Rather than requiring you to use the correct format specifiers in the format string, followed with a comma-separated list of arguments to substitute in, you simply enclose each argument in parentheses prefixed by a backslash. www.it-ebooks.info

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.