Sams Teach Yourself Swift in 24 Hours (Learning Lab)

 

Author: BJ Miller
Publisher: Sams Publishing
ISBN: 978-0-13-413324-9
Copyright © 2015 by Pearson Education, Inc.

Contents at a Glance

Introduction

HOUR 1: Introducing the Swift Development Environment

HOUR 2: Learning Swift’s Fundamental Data Types

HOUR 3: Using Operators in Swift

HOUR 4: Working with Collection Types (Arrays and Dictionaries)

HOUR 5: Understanding Optional Values

HOUR 6: Controlling Program Flow with Conditionals

HOUR 7: Iterating Code with Loops

HOUR 8: Using Functions to Perform Actions

HOUR 9: Understanding Higher Order Functions and Closures

HOUR 10: Learning About Structs and Classes

HOUR 11: Implementing Class Inheritance

HOUR 12: Harnessing the Power of Enums

HOUR 13: Customizing Initializers of Classes, Structs, and Enums

HOUR 14: Digging Deeper with Properties

HOUR 15: Adding Advanced Type Functionality

HOUR 16: Understanding Memory Allocation and References

HOUR 17: Using Protocols to Define Behavior

HOUR 18: Using Extensions to Add Type Functionality

HOUR 19: Working with Optional Chaining

HOUR 20: Introducing Generics

HOUR 21: Adding Interoperability with Objective-C

HOUR 22: Interacting with User Interfaces

HOUR 23: Asynchronous Programming in Swift

HOUR 24: Learning Swift’s Standard Library Functions

Introduction

At Apple’s yearly World Wide Developer Conference (WWDC) in June 2014, Apple announced a new programming language called Swift that the company had been developing since 2010. This was a huge announcement; Objective-C had been the primary language of choice for developing most Mac and iOS apps for many years. The excitement surrounding this language was palpable. Twitter lit up with tweets about Swift, domain names with Swift in the title were being bought up left and right, and within 24 hours of the announcement more than 300,000 copies of Apple’s Swift iBook had been downloaded. People were ready for change.

But a new language brings not only syntactic differences but also idiomatic differences and new conventions. Swift is not just an object-oriented language, but it introduces features gleaned from other languages, such as C#, Haskell, Ruby, and more. Touted to be “Objective-C without the C,” Swift builds upon familiar concepts from Objective-C but includes a more modern, safer syntax and multiple paradigms such as object-oriented, functional, imperative, and block structured.

Swift is officially at version 1.0 but is still evolving, and even as this book is being written more changes are entering beta. With that said, this book is current as of Swift 1.0 and Xcode 6.0.1. If there are changes that you find in these examples that do not work as described or with screenshots, please check Apple’s release documentation and electronic versions of this book as they can get updated a lot faster than the printed book you may have in your hands. Also, all the code examples from this book are available and will be kept up-to-date in the GitHub repository, https://github.com/STYSwiftIn24H/Examples.

Swift is already proving to be a great language and as of its release is compatible with iOS 7 and up. Swift can be written for apps running on OS X Yosemite, but as of Xcode 6.0.1, the option for adding Swift files to an Objective-C project, or even creating a Mac app using Swift are not available. More Mac support is coming in future releases. Updates are coming from Apple rather quickly, so if something is not available that you need or if something is not working as expected, consider filing a bug or feature request at http://bugreport.apple.com.

Who Should Read This Book?

This book is designed for a beginner-intermediate level programmer. Even advanced programmers who are not yet familiar with Swift can benefit from this book. You do not have to have a background in software development to make your way through this book, although it may help. If you are not familiar with software development whatsoever, you may benefit from more fundamental books first, although with the examples inside this book you may be able to follow along just fine.

In this book, I assume you have a passion to learn about Swift and to develop apps for the Mac and/or iOS platforms. I also assume that you are willing to carve out time in your schedule to take this book seriously and learn the concepts herein.

Note: Code-Continuation Arrows and Listing Line Numbers

You’ll see code-continuation arrows Image occasionally in this book to indicate when a line of code is too long to fit on the printed page.

Also, many listings have line numbers and some do not. The listings that have line numbers have them so that I can reference code by line; the listings that do not have line numbers are not called out by line.

What Should You Expect from This Book?

This book is a guided tour of the Swift programming language, discussing some of the ins-and-outs of Swift, best practices, do’s-and-don’ts, and more. It is not just a language reference. By the time you complete this book you should have a firm grasp on many of the concepts in Swift including the syntax to make them come to fruition.

You should not expect to be able to write award-winning iOS or Mac apps right out of the gate by just reading this book alone, as this book is not meant to be a one-stop-shop for learning everything about app creation. Such a book would be thousands of pages long. Rather, there are more components to writing apps, particularly the Cocoa and Cocoa Touch frameworks, which deserve books in their own right (and many exist). Apps should be written with careful planning and development, and depending on how many different technologies your app includes, you may need more resources.

You also do not need to read this book from cover to cover before attempting to write apps of your own using Swift. Feel free to experiment along the way with your own apps, or use this book for reference if you are stuck in an app of your own and need some guidance.

Also remember that this book is current as of Swift 1.0 and Xcode 6.0.1, so please understand that changes may be made after this book has gone through final edits and been printed. Code examples will be updated as progressions in the Swift language and Xcode environment change. They are available on GitHub at https://github.com/STYSwiftIn24H/Examples.


About the Author

BJ Miller is an iOS developer for a consultancy in the Cleveland, Ohio, area. BJ earned his B.S. in Computer Science from Baldwin-Wallace College (now called Baldwin-Wallace University) in Berea, Ohio, the town where he grew up. His latest career path encompasses large-scale enterprise network administration, SQL database administration, and Microsoft SharePoint Server and Microsoft Project Server administration and integration as a contractor for the United States Department of Defense, with all the Microsoft certifications that come along with that. Before that, he spent several years in LAN engineering, designing and implementing network infrastructure, as a Cisco Certified Network Associate.

BJ began iOS development in 2009 after not having programmed for a few years, and he developed a passion for the platform and the Objective-C language. Now, his love has expanded to include Swift, and there is still yet room in his heart for more. In 2013 he released his first app into the iOS App Store, called MyPrayerMap, as a simple tool for managing prayer requests.

When he is not writing in Objective-C or Swift for either work or this book, he enjoys spending time with his wife and two boys, reading, listening to music or podcasts, and playing The Legend of Zelda (any game on any system will do). He also co-organizes the Cleveland CocoaHeads Meetup with Daniel Steinberg, http://www.meetup.com/Cleveland-CocoaHeads/, and organizes a submeetup of that group called Paired Programming Fun, which is a casual meetup where the focus is on Test-Driven Development (TDD) in Swift and Objective-C in paired-programming style. BJ often presents iOS-related topics at CocoaHeads and also speaks at other conferences such as CocoaConf (Columbus, Ohio) and CodeMash v2.0.1.5. He also blogs from time to time at http://bjmiller.me and is on Twitter as @bjmillerltd.

Dedication

This book is dedicated to my wonderful family and friends who have been incredibly supportive throughout this entire process. Thank you all for your love and encouragement.

Acknowledgments

I would like to thank my wife and two boys for putting up with me while I wrote this book. I am excited to spend more time with you. I would also like to thank whoever invented coffee; may the Lord bless your soul and keep you. Speaking of the Lord, it is pretty close to not humanly possible that this book would have been completed in time, with all the other obstacles going on in my life, without his loving arms around me and my family; thank you, Jesus.

I would also like to thank my friends, coworkers, and the rest of the Mac/iOS community for all their love and encouragement. If I had not been introduced to someone, who introduced me to someone, who introduced me to Daniel Steinberg, I might not have pursued iOS development further and I might not have written this book. If you ever get the chance to meet that man, your life will be enriched.

Also, I cannot go without thanking the fine people who helped this book come to be: Trina MacDonald (Acquisitions Editor), Chris Zahn (Senior Development Editor), Olivia Basegio (Editorial Assistant), Hank Turowski (Technical Editor), Geneil Breeze (Copy Editor), and Betsy Gratner (Project Editor).

We Want to Hear from You!

As the reader of this book, you are our most important critic and commentator. We value your opinion and want to know what we’re doing right, what we could do better, what areas you’d like to see us publish in, and any other words of wisdom you’re willing to pass our way.

We welcome your comments. You can email or write to let us know what you did or didn’t like about this book—as well as what we can do to make our books better.

Please note that we cannot help you with technical problems related to the topic of this book.

When you write, please be sure to include this book’s title and author as well as your name and email address. We will carefully review your comments and share them with the author and editors who worked on the book.

Email:   errata@informit.com

Mail:      Sams Publishing
               ATTN: Reader Feedback
               330 Hudson Street
               7th Floor
               New York, New York 10013

Reader Services

Visit our website and register this book at informit.com/register for convenient access to any updates, downloads, or errata that might be available for this book.