O'Reilly logo

Swift High Performance by Kostiantyn Koval

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required