Book Image

Swift High Performance

By : Kostiantyn Koval
Book Image

Swift High Performance

By: Kostiantyn Koval

Overview of this book

Swift is one of the most popular and powerful programming languages for building iOS and Mac OS applications, and continues to evolve with new features and capabilities. Swift is considered a replacement to Objective-C and has performance advantages over Objective-C and Python. Swift adopts safe programming patterns and adds modern features to make programming easier, more flexible, and more fun. Develop Swift and discover best practices that allow you to build solid applications and optimize their performance. First, a few of performance characteristics of Swift will be explained. You will implement new tools available in Swift, including Playgrounds and REPL. These will improve your code efficiency, enable you to analyse Swift code, and enhance performance. Next, the importance of building solid applications using multithreading concurrency and multi-core device architecture is covered, before moving on to best practices and techniques that you should utilize when building high performance applications, such as concurrency and lazy-loading. Finally, you will explore the underlying structure of Swift further, and learn how to disassemble and compile Swift code.
Table of Contents (15 chapters)
Swift High Performance
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Preface

During the WWDC in June 2014, Apple announced a new programming language, called Swift. Swift is a very modern and powerful language. In the last year, Swift has become a very popular programming language. It has evolved and changed. Since Swift is fairly new, there are many questions related to its performance characteristics and best practice for achieving high performance in it.

Swift High Performance provides an overview of the important features of Swift, its performance characteristics, and sets of advices and techniques that allow you to build solid and sustainable applications in Swift with great performance.

This book also provides an overview of different tools that help you debug, investigate, and improve your code.

What this book covers

Chapter 1, Exploring Swift's Power and Performance, introduces Swift, its powerful features, its performance, and its interoperability with Objective-C.

Chapter 2, Making a Good Application Architecture in Swift, covers in detail the powerful features of Swift and how to apply them to build a solid application architecture.

Chapter 3, Testing and Identifying Slow Code with the Swift Toolkit, introduces different Swift and Xcode tools for code prototyping, performance measurement, and identifying and improving slow code.

Chapter 4, Improving Code Performance, shows Swift's performance-related details and features and demonstrates how Swift achieves its high performance. This chapter also covers different optimization techniques for improving performance in your applications.

Chapter 5, Choosing the Correct Data Structure, covers different data structures, their features, their performance characteristics, and suggestions on when to apply them.

Chapter 6, Architecting Applications for High Performance, demonstrates different application architecture techniques that allow you to achieve high performance, such as concurrency, avoiding state, and single responsibility.

Chapter 7, The Importance of Being Lazy, covers important techniques for improving an application's performance, such as lazy loading, lazy collections, and evaluation.

Chapter 8, Discovering All the Underlying Swift Power, gives you more details of Swift's structure, its tools, and the compilation process, and gives a better understanding of how Swift achieves its performance.

What you need for this book

This book's content and code examples were written using Xcode 7 and Swift 2.0. To follow along with the tutorials, you will need the following:

  • Mac OS 10.9 or later: Currently the Swift IDE, compiler, and tools are only available for Mac OS.

  • Xcode 7.0 or later: Xcode is the main development tool for Swift iOS and Mac apps. It can be installed via the Mac AppStore at https://itunes.apple.com/en/app/xcode/id497799835?mt=12.

  • Command-line tools for Xcode and simulators: Once you have installed Xcode and launched it, it will offer to install additional command-line tools. Xcode installs simulators by default, but you can download more simulators by going to Xcode | Preferences | Download.

Who this book is for

This book is for developers who already know the basics of Swift and want to learn the more advanced features and tips on how to achieve high performance and build solid applications in Swift. We assume that you are familiar with at least a bit of Mac OS and the Xcode IDE. This book is for everyone who wants to takes their knowledge of Swift to a new level.

Knowledge of iOS or Mac OS programming and Objective-C would be plus, but it's not required.

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: "First, let's add a nickname to the Person class."

A block of code is set as follows:

var sam = Person(firstName: "Sam", lastName: "Bosh", 
  nickName:"BigSam")
sam = sam.changeNickName("Rockky")

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

<type parameter : constraint >

func minElem<T : Comparable>(x: T, _ y: T) -> T {
  return x < y ? x : y
}

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

(lldb) repl
1> func isAllPositive(ar: [Int]) -> Bool { 
2.   let negatives = ar.filter { $0 < 0 }
3.   return negatives.count == 0
4. }

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: "We will choose a Time Profiler template and click on Record."

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, or you can visit https://github.com/kostiakoval/SwiftHighPerformance, which is the GitHub repo with code examples for this book.

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.