Author: BJ Miller Publisher: Sams Publishing ISBN: 978-0-13-443125-3Copyright © 2016 by Pearson Education
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
HOUR 5: Controlling Program Flow with Conditionals
HOUR 6: Understanding Optional Values
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: Understanding Protocol-Oriented Programming
HOUR 22: Handling Errors
HOUR 23: Adding Interoperability with Objective-C
HOUR 24: Functional Thinking in Swift
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 purchased 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 has evolved so much over the past year that it is difficult sometimes to see any similarities. 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, as well as reintroducing itself at WWDC 2015 as a protocol-oriented programming language.
Swift is officially at version 2.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 2.0 and Xcode 7. 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/ExamplesV2.
Swift is already proving to be a great language and as of its release is compatible with iOS 7 and up. Swift can also be written for apps running on OS X Yosemite and later. 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.
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.
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.
Note: Code-Continuation Arrows and Listing Line Numbers
You’ll see code-continuation arrows 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.
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). You should write apps via 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 2.0 and Xcode 7, 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/ExamplesV2.
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.
BJ Miller is an iOS developer for DXY Solutions, a mobile, web, and design 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 encompassed 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 in paired-programming style. BJ often presents iOS-related topics at CocoaHeads and also speaks at other conferences such as MacTech, 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.
I would like to thank my wife and two boys for putting up with me while I wrote this book...again. While this book was largely an update versus brand-new content, it was still an exhausting process. 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), Valerie Shipbaugh (Technical Editor), Paula Lowell (Copy Editor), and Betsy Gratner (Project Editor).
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.
You can email or write directly 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 stronger.
Please note that we cannot help you with technical problems related to the topic of this book, and that due to the high volume of mail we receive, we might not be able to reply to every message.
When you write, please be sure to include this book’s title and author, as well as your name and contact information.
Email: errata@informit.com
Mail: Sams Publishing
ATTN: Reader Feedback
330 Hudson Street
7th Floor
New York, New York 10013
Register your copy of Sams Teach Yourself Swift in 24 Hours at informit.com for convenient access to downloads, updates, and corrections as they become available. To start the registration process go to informit.com/register and log in or create an account.* Enter the product ISBN 9780672337659 and click submit. Once the process is complete, you will find any available bonus content under “Registered Products.”
*Be sure to check the box that you would like to hear from us in order to receive exclusive discounts on future editions of this product.