Swift Style
An Opinionated Guide to an Opinionated Language
by Erica Sadun
Version: P1.0 (March 2017)
Copyright 2017 The Pragmatic Programmers, LLC. This book is licensed to the individual who purchased it. We don't copy-protect it because that would limit your ability to use it for your own purposes. Please don't break this trustyou can use this across all of your devices but please do not share this copy with other members of your team, with friends, or via file sharing services. Thanks.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and The Pragmatic Programmers, LLC was aware of a trademark claim, the designations have been printed in initial capital letters or in all capitals. The Pragmatic Starter Kit, The Pragmatic Programmer, Pragmatic Programming, Pragmatic Bookshelf and the linking g device are trademarks of The Pragmatic Programmers, LLC.
Every precaution was taken in the preparation of this book. However, the publisher assumes no responsibility for errors or omissions, or for damages that may result from the use of information (including program listings) contained herein.
About the Pragmatic Bookshelf
The Pragmatic Bookshelf is an agile publishing company. Were here because we want to improve the lives of developers. We do this by creating timely, practical titles, written by programmers for programmers.
Our Pragmatic courses, workshops, and other products can help you and your team create better software and have more fun. For more information, as well as the latest Pragmatic titles, please visit us at http://pragprog.com.
Our ebooks do not contain any Digital Restrictions Management, and have always been DRM-free. We pioneered the beta book concept, where you can purchase and read a book while its still being written, and provide feedback to the author to help make a better book for everyone. Free resources for all purchasers include source code downloads (if applicable), errata and discussion forums, all available on the book's home page at pragprog.com. Were here to make your life easier.
New Book Announcements
Want to keep up on our latest titles and announcements, and occasional special offers? Just create an account on pragprog.com (an email address and a password is all it takes) and select the checkbox to receive newsletters. You can also follow us on twitter as @pragprog.
About Ebook Formats
If you buy directly from pragprog.com, you get ebooks in all available formats for one price. You can synch your ebooks amongst all your devices (including iPhone/iPad, Android, laptops, etc.) via Dropbox. You get free updates for the life of the edition. And, of course, you can always come back and re-download your books when needed. Ebooks bought from the Amazon Kindle store are subject to Amazon's polices. Limitations in Amazon's file format may cause ebooks to display differently on different devices. For more information, please see our FAQ at pragprog.com/frequently-asked-questions/ebooks. To learn more about this book and access the free resources, go to https://pragprog.com/book/esswift, the book's homepage.
Thanks for your continued support,
Andy Hunt
The Pragmatic Programmers
The team that produced this book includes: Andy Hunt (Publisher) Janet Furlow (VP of Operations) Susannah Davidson Pfalzer (Executive Editor) Brian MacDonald (Development Editor) Potomac Indexing, LLC (Indexing) Linda Recktenwald (Copy Editor) Gilson Graphics (Layout)
For customer support, please contact .
For international rights, please contact .
This book is dedicated to the Swift community both inside and outside Apple.
Table of Contents
Copyright 2017, The Pragmatic Bookshelf.
Early Praise for Swift Style
Writing a style guide is tricky, because developers have such strong opinions on style that it can be scary to jump into the fray. But in this book, Erica is brave enough to offer strong personal recommendations and opinions based on battle-won experience, and she backs them up with solid arguments and reasoning, while ultimately encouraging you to make your own decisions. If youre looking for an in-depth exploration of Swift style, this is the book for you. Youre guaranteed to improve your styleand learn a few great tricks along the way!
Ray Wenderlich |
Founder, Razeware LLC |
I really enjoyed reading this bookits comprehensive and thorough while staying concise. The book reflects the growing Swift community, including the open source world. Importantly, rationale is presented for everything discussed, which makes for a nuanced and educational book. I felt a lot of my assumptions being challenged, and while I didnt always agree with the conclusions of the book, I was always left with something to think about. And, of course, I learned a lot too. Id highly recommend this to any Swift developer.
Ash Furrow |
Developer, Artsy |
When I hear the term style guide, I imagine a burdensome block of dictates, laid down by some mid-career code Stalinist still trying to win an office argument from 2005. Thats what makes Erica Saduns Swift Style such a delightful surprise. She brings out the natural charm and sensibility of Swift with clear examples, practical reasoning, and a healthy bit of whimsy.
Chris Adamson |
Author, iOS SDK 10 Development |
Written by one of the Swift communitys most respected and active contributors, and with substantial care and attention to detail, this is a concise and thoughtful exploration of the many issues to be considered when developing your own Swift style. This is a worthy addition to any Swift developers library, and one thats sure to help you write better, more maintainable code.
Anthony Gray |
Author, Swift Pocket Reference |
Swift Style is one of those rare must have booksa style guide filled with opinions, options, and history written by someone who helped shape the language. We each benefit when all of us follow these guidelines. Every Swift developer will find themselves referring to this book often. It is our Strunk and White .
Daniel Steinberg |
Trainer, speaker, author, Dim Sum Thinking |
Swift Style was everything I had hoped: carefully considered, straight to the point, and most importantly, immediately applicable to any serious project.
Paul Hudson |
Author, hackingwithswift.com |
This is a book about the why, not the what. Even if you change nothing about how you write Swift, Swift Style will make you think about why you make a stylistic choice and the impact thatll have when you (or someone else) have to read your code later.
Christina Moulton |
President, Teak Mobile, Inc. |
Welcome to Swift Style
This book offers a practical, powerful, and opinionated guide to coding style. It incorporates a multitude of best practices, guiding you to work successfully in this equally opinionated programming language.
How This Book Got Here
This book didnt start out as a book. It began its life as a blog post (Swift: Dont do that Murashev writes a weekly curated Swift newsletter that covers news and articles about the language.