Learning Swift - Second Edition
eBook - ePub

Learning Swift - Second Edition

Andrew J Wagner

Share book
  1. 308 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Learning Swift - Second Edition

Andrew J Wagner

Book details
Book preview
Table of contents
Citations

About This Book

Develop the skills required to create compelling, maintainable, and robust iOS and OS X apps with SwiftAbout This Book• Write expressive, understandable, and maintainable Swift 2 code with this hands-on tutorial• Unveil the complex underpinnings of Swift to turn your app ideas into reality• This book is packed with real-life examples to help you implement concepts as you learnWho This Book Is ForIf you are looking to build iOS or OS X apps using the most modern technology, this book is ideal for you. You will find this book especially useful if you are new to programming or if you are yet to develop for iOS or OS X. No prior programming exposure is required.What You Will Learn• Form a solid understanding of the Swift 2 language• Get to know the practical aspects of how a computer program actually works• Understand the paradigms used by Apple's frameworks so you are not intimidated by them• Utilize the vast resources written in Objective-C to better inform your Swift programming• Develop a basic portfolio of Swift code by learning the critical concepts• Experience both object-oriented and functional programming• Get to know the new coding techniques made available by Swift 2• Discover resources to ensure you never stop becoming a better developerIn DetailSwift is Apple's new programming language and the future of iOS and OS X app development. It is a high-performance language that feels like a modern scripting language. On the surface, Swift is easy to jump into, but it has complex underpinnings that are critical to becoming proficient at turning an idea into reality.This book is an approachable, step-by-step introduction into programming with Swift for everyone. It begins by giving you an overview of the key features through practical examples and progresses to more advanced topics that help differentiate the proficient developers from the mediocre ones. It covers important concepts such as Variables, Optionals, Closures, Generics, and Memory Management. Mixed in with those concepts, it also helps you learn the art of programming such as maintainability, useful design patterns, and resources to further your knowledge. This all culminates in writing a basic iOS app that will get you well on your way to turning your own app ideas into reality.Style and approachThis is an approachable, step-by-step guide to programming in Swift 2. Each topic is separated into compressible sections that are full of practical examples and easy-to-understand explanations. Each section builds on the previous topics so you can develop a proficient and comprehensive understanding of app development in Swift 2.

Frequently asked questions

How do I cancel my subscription?
Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
Can/how do I download books?
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Do you support text-to-speech?
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Is Learning Swift - Second Edition an online PDF/ePUB?
Yes, you can access Learning Swift - Second Edition by Andrew J Wagner in PDF and/or ePUB format, as well as other popular books in Computer Science & Programming Mobile Devices. We have over one million books available in our catalogue for you to explore.

Information

Year
2016
ISBN
9781785883811
Edition
2

Learning Swift Second Edition


Table of Contents

Learning Swift Second Edition
Credits
About the Author
About the Reviewer
www.PacktPub.com
eBooks, discount offers, and more
Why subscribe?
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
Downloading the color images of this book
Errata
Piracy
Questions
1. Introducing Swift
Defining our goals for this book
Setting up the development environment
Running our first swift code
Understanding playgrounds
Learning with this book
Summary
2. Building Blocks – Variables, Collections, and Flow Control
Core Swift types
Constants and variables
Containers
Tuples
Arrays
Dictionaries
Swift's type system
Printing to the console
Control flow
Conditionals
Switches
Loops
Functions
Basic functions
Parameterized functions
Functions that return values
Functions with default arguments
Guard statement
Bringing it all together
Summary
3. One Piece at a Time – Types, Scopes, and Projects
Structs
Types versus instances
Properties
Member and static methods
Computed properties
Reacting to property changes
Subscripts
Custom initialization
Classes
Inheriting from another class
Initialization
Overriding initializer
Required initializer
Designated and convenience initializers
Overriding methods and computed properties
Methods
Computed properties
Casting
Upcasting
Downcasting
Enumerations
Basic declaration
Testing enumeration values
Raw values
Associated values
Methods and properties
Projects
Setting up a command-line Xcode project
Creating and using an external file
Interfacing with code from other files
File organization and navigation
Extensions
Scope
How scope is defined
Nested types
Access control
Summary
4. To Be or Not To Be – Optionals
Defining an optional
Unwrapping an optional
Optional binding
Forced unwrapping
Nil coalescing
Optional chaining
Implicitly unwrapped optionals
Debugging optionals
The underlying implementation
Summary
5. A Modern Paradigm – Closures and Functional Programming
Functional programming philosophy
State and side effects
Declarative versus imperative code
Closures
Closures as variables
Closures as parameters
Syntactic sugar
Building blocks of functional programming in Swift
Filter
Reduce
Map
Sort
How these affect the state and nature of code
Lazy evaluation
Example
Summary
6. Make Swift Work For You – Protocols and Generics
Protocols
Defining a protocol
Implementing a protocol
Using type aliases
Generics
Generic function
Generic type
Type constraints
Protocol constraints
Where clauses for protocols
Where clauses for equality
Extending generics
Adding methods to all forms of a generic
Adding methods to only certain instances of a generic
Extending protocols
Putting protocols and generics to use
Generators
Sequences
Product of Fibonacci numbers under 50
Summary
7. Everything Is Connected – Memory Management
Computer data storage
File system
Memory
Value types versus reference types
Determining value type or reference type
Behavior on assignment
Behavior on input
Closure capture behavior
Automatic reference counting
Object relationships
Strong
Weak
Unowned
Strong reference cycles
Between objects
Spotting
Fixing
With closures
Spotting
Fixing
Lost objects
Between objects
With closures
Structures versus classes
Summary
8. Paths Less Traveled – Error Handling
Throwing errors
Defining an error type
Defining a function that throws an error
Implementing a function that throws an error
Handling errors
Forceful try
Optional try
Catching an error
Propagating errors
Cleaning up in error situations
Order of execution when errors occur
Deferring execution
Summary
9. Writing Code the Swift Way – Design Patterns and Techniques
What is a design pattern?
Behavioral patterns
Iterator
Observer
Callback
Notification center
Structural patterns
Composite
Hierarchies
Alternative to subclassing
Delegate
Model view controller
Creational patterns
Singleton/shared instance
Abstract factory
Using associated values effectively
Replacing class hierarchies
Concisely representing state
Extending system types to reduce code
Lazy properties
Avoiding unnecessary memory usage
Avoiding unnecessary processing
Localizing logic to the concerned property
Summary
10. Harnessing the Past – Understanding and Translating Objective-C
Swift's relationship to Objective-C
Background of Objective-C
Constants and variables
Value types
Reference types
Containers
Arrays
Dictionaries
Control flow
Conditionals
Switches
Loops
Functions
Types
Structures
Enumerations
Classes
Basic class
Initializers
Properties
Methods
Inheritance
Categories
Protocols
Blocks
Projects
Header files
Implementation files
Organization
Calling Objective-C code from Swift
Bridging header
Using functions
Using types
Containers
Annotations
Nullability
Container element types
Summary
11. A Whole New World – Developing an App
Conceptualizing the app
Features
Interface
Data
Setting up the app project
Configuring the user interface
Running the app
Allowing picture taking
Temporarily saving a photo
Populating our photo grid
Refactoring to respect model-view-controller
Permanently saving a photo
Summary
12. What's Next? – Resources, Advice, and the Next Steps
Apple's documentation
Forums and blogs
Blog posts
Forums
Prominent figures
Podcasts
Summary
Index

Learning Swift Second Edition

Copyright © 2016 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: June 2015
Second edition: March 2016
Production reference: 1170316
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78588-751-2
www.packtpub.com

Credits

Author
Andrew J Wagner
Reviewer
Guan Gui
Commissioning Editor
Kartikey Pandey
Acquisition Editors
Vivek Anantharaman
Chaitanya Nair
Content Development Editor
Viranchi Shetty
Technical Editor
Saurabh Malhotra
Copy Editors
Kevin McGowan
Sneha Singh
Project Coordinator
Izzat Contractor
Proofreader
Safis Editing
Indexer
Hemangini Bari
Production Coordinator
Shantanu N. Zagade
Cover Work
Shantanu N. Zagade

About the Author

Andrew J Wagner is a software developer who concentrates on iOS development and backend web services. He has a degree in computer engineering from Rensselaer Polytechnic Institute, New York. Currently, he works for a development shop named Chronos Interactive based in Denver, CO. He has experience of working with and for large-scale and small-scale companies, as well as running his own contracting and app companies. He is passionate about using computers as a creative outlet and writing software that is beautiful in implementation, functionality, and experience.
When he isn't working or spending time with friends and family, he writes for his blog at http://drewag.me.

Table of contents