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

Avoiding state


The first rule is to avoid a state. When you are doing two things at the same time, those two processes should be as independent and isolated as possible. They shouldn't know anything about each other or share any mutable resources. If they do, then we would need to take care of synchronizing access to that shared resource, which would bring a complexity to our system that we don't want. Right now in our code we have two states: a revenue numbers array and the average result. Both of the processes have access to that state.

The first problem in that the code is referencing itself. When you try to access an instance variable or a method that is out of the closure scope, you see an error message: Reference to property 'revenue' in closure requires explicit 'self.' to make capture semantics explicit.

Xcode would also propose a fix to this issue, adding explicit self-capturing. This would solve the Xcode error but it wouldn't solve the root problem. When you see this error, stop...