26,39 €
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.
Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:
Seitenzahl: 229
Veröffentlichungsjahr: 2015
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: November 2015
Production reference: 1271015
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78528-220-1
www.packtpub.com
Author
Kostiantyn Koval
Reviewers
Ravi Shankar
Tatsuya Tobioka
Ye Xiaodong
Acquisition Editor
Reshma Raman
Content Development Editor
Dharmesh Parmar
Technical Editor
Shiny Poojary
Copy Editors
Stephen Copestake
Vikrant Phadke
Project Coordinator
Izzat Contractor
Proofreader
Safis Editing
Indexer
Rekha Nair
Production Coordinator
Melwyn Dsa
Cover Work
Melwyn Dsa
Kostiantyn Koval is a passionate developer with 5 years of experience. All the time, his main passion and work has been building iOS applications. So far, he has built many different applications, including games, enterprise apps, and big platforms. He fell in love with Swift the first minute he saw it, and keeps expressing this to the rest of the world.
Other than iOS, he is also interested in technologies and languages such as Scala, Clojure, LLVM, Ruby, JavaScript, and others.
He loves open source and blogging. You can find him on GitHub at https://github.com/kostiakoval and his blogs at https://medium.com/@kostiakoval. Other places to contact him are http://kostiakoval.github.io and Twitter at @KostiaKoval.
His hobbies are programming, building start-ups, and making this world better with software.
I really want to say thanks to my wife, Tetiana, for believing in me, supporting me, and helping me in life, especially during the writing of this book.
Ravi Shankar is a multi-skilled software consultant with over 15 years of experience in the IT industry. He has good all-around ability in different technologies and extensive experience in product development, system maintenance, and support. He is a polyglot and self-taught programmer with hands-on experience in Swift, Objective-C, and Java. Ravi believes in gaining knowledge through sharing and helping others learn.
Thanks to Izzat and Packt Publishing for giving me this opportunity.
Tatsuya Tobioka is a software engineer familiar with Ruby, JavaScript, Objective-C, and Swift. He lives happily with his beloved wife and children in Tokyo, Japan.
He started iOS development in 2010, and then released a number of apps for developers, such as JavaScript Anywhere, Edhita, and CoffeeScript At Once.
Currently, Tatsuya spends much of his time learning tvOS.
You can check out his open source projects on GitHub at https://github.com/tnantoka and tweets at @tnantoka.
Ye Xiaodong is a full-stack software engineer and technical director of zai360.com (http://www.zai360.com/), an O2O company that provides recyclable collection services for Chinese family customers on a periodical basis. He has 7 years of application development experience working for start-ups and leading companies across the world with iOS, Android, Windows Phone, Symbian, and Meego. He has developed lots of iOS applications; designed, created, and maintained iOS libraries and Xcode plugins; and contributed to open source projects. Ye is passionate about bringing the latest features into applications. He was a technical reviewer for Mastering Swift and Swift Design Patterns, Packt Publishing.
First and foremost, I would like to thank the coordinator of this project, Izzat Contractor, for her valuable guidance and advice.
For support files and downloads related to your book, please visit www.PacktPub.com.
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 <[email protected]> for more details.
At www.PacktPub.com, 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.
If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view 9 entirely free books. Simply use your login credentials for immediate access.
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.
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.
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:
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.
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 <[email protected]>, 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.
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.
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.
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 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 <[email protected]> with a link to the suspected pirated material.
We appreciate your help in protecting our authors and our ability to bring you valuable content.
If you have a problem with any aspect of this book, you can contact us at <[email protected]>, and we will do our best to address the problem.
In the 2014, Apple released a new programming language, called Swift. Swift has been designed from scratch with many powerful features. It is statically typed and very safe. It has a clean and nice syntax, it's fast, it's flexible, and it has many other advantages that you will learn later in the book. Swift seems to be very powerful and it has big potential. Apple has set big expectations for Swift, and their main goal for Swift is that it should be a replacement for Objective-C, which is going to happen in the near future.
In this chapter, you will become familiar with the Swift programming language, what it was made for, and what its advantages and features are. We will also make our first Swift application and see how easy it is to integrate with existing Objective-C code.
In this chapter, we will cover the following topics:
I can guess you opened this book because you are interested in speed and are probably wondering, "How fast can Swift be?" Before you even start learning Swift and discovering all the good things about it, let's answer it right here and right now.
Let's take an array of 100,000 random numbers; sort it in Swift, Objective-C, and C using the standard sort function from stdlib (sort in Swift, qsort in C, and compare in Objective-C); and measure how much time each would take.
Sorting an array with 100,000 integer elements gives us this:
Technology
Time taken
Swift
0.00600 sec
C
0.01396 sec
Objective-C
0.08705 sec
And the winner is, Swift! Swift is 14.5 times faster than Objective-C and 2.3 times faster than C.
In other examples and experiments, C is usually faster than Swift and Swift is way faster than Objective-C. These measurements were done with Xcode 7.0 beta 6 and Swift 2.0. It's important to highlight that the improvements in Swift 2.0 were mainly focused on making it cleaner, more powerful, safer, and more stable, and preparing it for open sourcing. Swift's performance hasn't reached its full potential yet, and the future is so exciting!
The Swift programming language has been designed by Apple from the ground up. It was released with the slogan Objective-C without the C. The meaning of this phrase is that Swift doesn't have any limitation of backward compatibilities. It's totally new and with no old baggage. Before you start learning all the power of Swift, I think it would be useful to answer a few questions about why should you learn it, and if you have any doubts about that, I should dispel them.
Swift is a very new programming language but it has become very popular and has gained huge traction. However, many iOS and OS X developers ask these questions:
My answer is, "Yes. Definitely!" You should learn Swift. It doesn't matter whether you are a new iOS and OS X developer or you have some Objective-C background; you should definitely learn Swift.
If you are new developer, then it's really useful to start with Swift, because you will learn programming basics and techniques in Swift, and further Swift learning would be much easier. Although it would definitely useful to learn Objective-C as well, I would recommend learning Swift first so that you build your programming mindset on Swift.
If you already have some experience in Objective-C, then you should try Swift as soon as possible. It will not only give you the knowledge of a new programming language, but also open the door to new ideas and ways of solving problems in Objective-C. We can see that Objective-C has started evolving right now because of Swift.
Objective-C has many limitations because of its backward capabilities with C. It was created 23 years ago, in 1983, but it will die much sooner than Swift.
After the release of Swift version 1.0, in only a year's time we have seen many Swift applications successfully developed and released on the App Store. In this time period, many Swift tools and open source libraries that increase development productivity have been created.
During WWDC 2015, Apple announced that Swift will be made open source. This means that Swift can be used to write any software and not only iOS or OS X apps. You can write a piece of server-side code or web app in Swift. This is one more reason you should learn it.
On the other hand, we see that Swift is under constant development. There were many changes and improvements in version 1.2, and there were even more changes in version 2.0. Although it's very easy to upgrade to the newer Swift version with the Xcode migrator, it's something you should think about.
Swift has some promising performance characteristics. We have seen a huge performance improvement in the Swift 1.2 release, and some improvements in Swift 2.0 as well. You have seen from the previous example how fast Swift is, and in general, Swift has more potential to achieve high performance than Objective-C.
Finally, I want to mention a phrase I really like, by Bryan Irace:
When the iOS SDK says "Jump", ask "How High?"
Don't wait, learn Swift!
