Chapter: Table of Contents Table of Contents Application Development with Swift Credits About the Author About the Reviewers www.PacktPub.com Support files, eBooks, discount offers, and more Why subscribe? Free access for Packt account holders Preface What this book covers What you need for this book Who this book is for Conventions Reader feedback Customer support Downloading the example code Errata Piracy Questions 1. Hello Swift Introduction to Swift Playgrounds The code structure Arrays Initializing an array Iterating over arrays Appending items Removing and updating items Dictionaries Initializing a dictionary Appending or updating values Removing items from the dictionary Enum Using enumerations with the switch statement Enumerations with associated values Functions Closures Summary 2. Advanced Swift Type casting and type checking Downcasting Using Any and AnyObject Protocols Properties Methods Class-only protocols Protocol compositions The protocol conformance The optional requirements Delegation Extensions Adding computed properties Adding new initializers Adding methods Generics Operator functions Memory management Using Objective-C and Swift in a single project Importing Objective-C in the Swift project Importing Swift in the Objective-C project Limitations Summary 3. Touch ID What is Touch ID? The LocalAuthentication framework Summary 4. Introduction to HealthKit What is HealthKit? HealthKit limitations HealthKit privacy Getting started with HealthKit Configuring the Xcode project Getting your hands dirty with HealthKit The HealthKit store Asking for permissions Updating the UI Sharing data in the Health app Summary 5. Introduction to Metal What is Metal? Features 3D rendering Pipeline Transformations Projection Other options to work with 3D rendering in iOS Getting started with Metal Devices and CAMetalLayers The command queue Vertex and color buffers Vertex and fragment shaders The render pipeline The game loop and rendering The display link The render pass The command buffer Summary 6. Introduction to WatchKit Apple watch WatchKit The WatchKit app Glances Actionable notifications Working with WatchKit Xcode targets Interface controllers Interface elements Groups Navigating interfaces Presenting modally Animation Working with glances Summary 7. Swift App Extensions App extensions Today Share Photo Editing Document Provider Understanding the Document Provider's lifecycle The custom keyboard Keyboard security Keyboard settings Working with the custom keyboard Demo Summary Index Chapter: Application Development with Swift Application Development with Swift Application Development with Swift Copyright © 2015 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. First published: August 2015 Production reference: 1250815 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78528-817-3 www.packtpub.com Chapter: Credits Credits Author Hossam Ghareeb Reviewers Tyler Hackbart David Mattia Hossam M. Sherif Kenneth Strickland Commissioning Editor Julian Ursell Acquisition Editor Nikhil Karkal Content Development Editor Shali Deeraj Technical Editor Prajakta Mhatre Copy Editor Charlotte Carneiro Project Coordinator Kinjal Bari Proofreader Safis Editing Indexer Tejal Soni Graphics Sheetal Aute Disha Haria Production Coordinator Melwyn D'sa Cover Work Melwyn D'sa Chapter: About the Author About the Author Hossam Ghareeb is a software engineer who graduated from the computer and system engineering department of Alexandria University in 2012. Currently, he is an iOS software engineer at Prototype Interactive in Dubai. He has a lot of experience in iOS development and software engineering; he always keeps himself up to date with new technologies. It is his passion to help people by sharing his experience and writing tutorials to get people engaged quickly without problems. His favorite hobbies are fishing and playing video games, especially FIFA and PES. If you need his help or wish to go fishing with him, then you can contact him at I would like to thank each and every person who has been helpful to me, from my teaching staff to my friends. A special thanks to my lovely wife, Amira, for all her support and encouragement in spite of all the time this book kept me away from her. I thank my son, Yusuf, for inspiring me. I would also like to express my sincere gratitude toward my parents for teaching me how to be a good person. Last but not least, I would like to thank the content editors, Shali Deeraj and Nikhil Karkal, for helping me and being nice to me, and the reviewers for their helpful comments and suggestions. Chapter: About the Reviewers About the Reviewers Tyler Hackbart is a web developer, designer, and technology enthusiast with a passion for everything in technology. Through self-learning, he gained the ability to write his first line of code when he was only 16 years old and has just started learning his sixth programming language. With his farm-raised work ethic, alongside a passion for crafting amazing user interactions and doing what he loves, Tyler has successfully worked in the technology field for 2 years. He is the cofounder of three start-ups and works in an application design firm, all based in Waterloo, Canada. David Mattia is a student at the University of Notre Dame with a passion for software engineering. He has worked for General Electric Aviation and spends much of his free time doing freelance work. Hossam M. Sherif is a software developer. He received his bachelor's degree in computer and system engineering from Alexandria University. He has spent several years in software development, especially in iOS and backend development. He has worked with many companies, such as Insidetrack, Inova LLC, and Log n Labs. He worked on different projects and in different roles, from software developer to team leader and manager. He served in the Egyptian Navy and participated in the enhancement and development of the IT department. He has certificates in software test processes and software test design. He participated in several volunteer projects and workshops as an instructor for "Introduction to iOS Development," a summer 2014 training course for undergraduates at Alexandria University, and also other courses, such as the Alexandria Startup 2012 weekend and IEEE ITW 2011. Kenneth Strickland joined the US Army in 1986. After 4 years of active duty in the US Army, he came across software development. That was when he picked up his first C programming book and read it from cover to cover. Since then, he has never looked back. He has been a software architect for over 10 years, and currently develops applications primarily in C/C++, Java, Objective-C, Swift, and .NET. You can visit his website at Chapter: www.PacktPub.com www.PacktPub.com Chapter: Support files, eBooks, discount offers, and more Support files, eBooks, discount offers, and more For support files and downloads related to your book, please visit Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at for more details. At you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks. https://www2.packtpub.com/books/subscription/packtlib Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can search, access, and read Packt's entire library of books. Chapter: Why subscribe? Why subscribe? Fully searchable across every book published by Packt Copy and paste, print, and bookmark content On demand and accessible via a web browser Chapter: Free access for Packt account holders Free access for Packt account holders If you have an account with Packt at you can use this to access PacktLib today and view 9 entirely free books. Simply use your login credentials for immediate access. Chapter: Preface Preface This book teaches you how to master the Swift programming language and use it in building apps with the new technologies introduced in iOS 8. Swift is a new programming language for iOS, OS X, and watchOS apps. In the beginning of this book, we will give you an advanced introduction to Swift, and then we will see how to apply this in real-app demos using the new technologies and APIs in iOS 8 such as TouchID, watchOS, Metal, and HealthKit. Chapter: What this book covers What this book covers Chapter Hello gives a quick introduction and revision about Swift. This chapter is very important for Swift beginners and is highly recommended for Swift developers to revise their knowledge. Chapter Advanced boosts your Swift experience by allowing you to learn some advanced features in Swift. In this chapter, we will talk about protocols, extensions, and memory management. Chapter: What you need for this book Chapter: Who this book is for Chapter: Conventions Chapter: Reader feedback Chapter: Customer support Chapter: Downloading the example code Chapter: Errata Chapter: Piracy Chapter: Questions Chapter: 1. Hello Swift Chapter: Introduction to Swift Chapter: Playgrounds Chapter: The code structure Chapter: Arrays Chapter: Initializing an array Chapter: Iterating over arrays Chapter: Appending items Chapter: Removing and updating items Chapter: Dictionaries Chapter: Initializing a dictionary Chapter: Appending or updating values Chapter: Removing items from the dictionary Chapter: Enum Chapter: Using enumerations with the switch statement Chapter: Enumerations with associated values Chapter: Functions Chapter: Closures Chapter: Summary Chapter: 2. Advanced Swift Chapter: Type casting and type checking Chapter: Downcasting Chapter: Using Any and AnyObject Chapter: Protocols Chapter: Properties Chapter: Methods Chapter: Class-only protocols Chapter: Protocol compositions Chapter: The protocol conformance Chapter: The optional requirements Chapter: Delegation Chapter: Extensions Chapter: Adding computed properties Chapter: Adding new initializers Chapter: Adding methods Chapter: Generics Chapter: Operator functions Chapter: Memory management Chapter: Using Objective-C and Swift in a single project Chapter: Importing Objective-C in the Swift project Chapter: Importing Swift in the Objective-C project Chapter: Limitations Chapter: Summary Chapter: 3. Touch ID Chapter: What is Touch ID? Chapter: The LocalAuthentication framework Chapter: Summary Chapter: 4. Introduction to HealthKit Chapter: What is HealthKit? Chapter: HealthKit limitations Chapter: HealthKit privacy Chapter: Getting started with HealthKit Chapter: Configuring the Xcode project Chapter: Getting your hands dirty with HealthKit Chapter: The HealthKit store Chapter: Asking for permissions Chapter: Updating the UI Chapter: Sharing data in the Health app Chapter: Summary Chapter: 5. Introduction to Metal Chapter: What is Metal? Chapter: Features Chapter: 3D rendering Chapter: Pipeline Chapter: Transformations Chapter: Projection Chapter: Other options to work with 3D rendering in iOS Chapter: Getting started with Metal Chapter: Devices and CAMetalLayers Chapter: The command queue Chapter: Vertex and color buffers Chapter: Vertex and fragment shaders Chapter: The render pipeline Chapter: The game loop and rendering Chapter: The display link Chapter: The render pass Chapter: The command buffer Chapter: Summary Chapter: 6. Introduction to WatchKit Chapter: Apple watch Chapter: WatchKit Chapter: The WatchKit app Chapter: Glances Chapter: Actionable notifications Chapter: Working with WatchKit Chapter: Xcode targets Chapter: Interface controllers Chapter: Interface elements Chapter: Groups Chapter: Navigating interfaces Chapter: Presenting modally Chapter: Animation Chapter: Working with glances Chapter: Summary Chapter: 7. Swift App Extensions Chapter: App extensions Chapter: Today Chapter: Share Chapter: Photo Editing Chapter: Document Provider Chapter: Understanding the Document Provider's lifecycle Chapter: The custom keyboard Chapter: Keyboard security Chapter: Keyboard settings Chapter: Working with the custom keyboard Chapter: Demo Chapter: Summary Chapter: Index