Book Image

Swift Cookbook

By : Cecil Costa, Cecil Costa
Book Image

Swift Cookbook

By: Cecil Costa, Cecil Costa

Overview of this book

Table of Contents (18 chapters)
Swift Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Preface

It was unbelievable in September 2014, when there were lots of apps available for iPhone and iPad, that Apple suddenly announced that there would be a new programming language for developing for iOS and OS X. So, Swift appeared and as with other languages, it came with its own peculiarities and tricks. This book is intended to update Objective-C developers who want to migrate to Swift and also to help Swift developers get a stronger base with better knowledge about this programming language.

If you enjoy creating small apps, this book is perfect for you. This book will show you how to create Swift apps from scratch. So, take your Mac, open your Xcode, and let's cook Swift!

What this book covers

Chapter 1, Getting Started with Xcode and Swift, introduces you to some Xcode features that are Swift-specific. This might sound a bit advanced for the first chapter, but it is not difficult and it is also very important, mainly for those people who would like to develop professionally.

Chapter 2, Standard Library and Collections, shows you how to manipulate arrays, dictionaries, sets, strings, and other objects using the Swift way. This chapter is very important for people who have been working with Objective-C.

Chapter 3, Using Structs and Generics, shows you how Swift structs are not the same as Objective-C (or even C) structs, and that generics is a feature that allows you to create functions that are not tied to only one type. Both features have their own tricks.

Chapter 4, Design Patterns with Swift, explains how to implement design patterns using Swift, especially if you like object-oriented programming.

Chapter 5, Multitasking, shows you how to use different types of multitasking, a feature that is present in almost every app nowadays.

Chapter 6, Playground, teaches you how to use Playground, a new Xcode feature that allows you to test your code before adding it to your project.

Chapter 7, Swift Debugging with Xcode, explains how to debug Swift code using Xcode, LLDB, and Instruments. Here, you learn some tricks for finding and solving bugs in your app.

Chapter 8, Integrating with Objective-C, shows you how Swift and Objective-C can live together and gives you a step-by-step guide on how to migrate your Objective-C app to Swift.

Chapter 9, Dealing with Other Languages, shows you how to use C, C++, and the assembly language with Swift since you already know Swift is not alone in iOS and OS X development.

Chapter 10, Data Access, shows you different ways of storing data, which can be local or remote.

Chapter 11, Miscellaneous, expounds some topics that are very important in the Swift development world, from new frameworks such as the WatchKit to the widely used ones.

What you need for this book

Developing on Swift requires Xcode 6 or higher, which by itself needs to be installed on Yosemite (OS X 10.10), which can only be installed on a Mac computer. So, this is basically what you need.

A few recipes can only be tested on a physical device (iPhone, iPad, or iPod); therefore, they can only be installed if you are enrolled on the Apple Developer Program.

Who this book is for

If you are an experienced Objective-C programmer and are looking for quick solutions to many different coding tasks in Swift, then this book is for you. You are expected to have development experience, though not necessarily with Swift.

Sections

In this book, you will find several headings that appear frequently (Getting ready, How to do it, How it works, and There's more).

To give clear instructions on how to complete a recipe, we use these sections as follows:

Getting ready

This section tells you what to expect in the recipe, and describes how to set up any software or any preliminary settings required for the recipe.

How to do it…

This section contains the steps required to follow the recipe.

How it works…

This section usually consists of a detailed explanation of what happened in the previous section.

There's more…

This section consists of additional information about the recipe in order to make the reader more knowledgeable about the recipe.

Conventions

In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "The XCPlayground also helped us with the XCPCaptureValue function."

A block of code is set as follows:

protocol Queue {
    typealias ElementType
    func enqueue(element:ElementType)
    func dequeue() -> ElementType
    var size: Int{
        get
    }
}

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

override func viewDidLoad() {
    super.viewDidLoad()
    // Perform custom UI setup here
    var geekNib = UINib(nibName: "Geekboard", bundle: nil)
    self.view = geekNib.instantiateWithOwner(self, options: nil)[0] as UIView
    self.label.text = currentBinaryText
    self.nextKeyboardButton = UIButton.buttonWithType(.System) as UIButton

Any command-line input or output is written as follows:

xcode-select -p

New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "When the application appears, click on calculate, without adding any information into the text fields."

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail , and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at , and we will do our best to address the problem.