Swift is a powerful and fun language. Its protocol-oriented nature allows you to do retroactive modeling and promotes composition over inheritance. In this series of posts, I will share Swift snippets that I’ve used across projects that I feel are really helpful in rapid development.
Due to the many screen sizes in the mobile world, staying relative to screen size is crucial. Hard-coding margins and sizes based on points can be short-sighted. In this post, we will subclass NSLayoutConstraint to achieve percentage-based margins.
Grand Central Dispatch (GCD) is a great technology provided by Apple. It provides an elegant level of abstraction to work with threads, queues, and locks. And it went through a much needed make-over in Swift 3. In this post, I would like to take this one step further using enums as a queue factory.
GCD is not for thread-unsafe shared resources since it does not guarantee the same thread will be used for the queue. We can use the threads API with a bit of sugar syntax.
Thread-safe resources in Swift can be achieved with Grand Central Dispatch. Using a concurrent queue and the barrier flag, reads can occur in parallel while writes are given mutual exclusivity for safety and optimization.
We’re still waiting for coroutines in Swift several years in. In the meantime, we have many concurrency mechanisms to choose from. How do we know which one to choose? Let’s examine each one and compare their performances.