Now this is a truly epic investigation of the evolution of how to write a singleton in Swift:
THE RIGHT WAY A.K.A. “THE ONE LINE SINGLETON (NOW WITH PROOF!”)
We knew about this “right” method of writing singletons, but we had no proof to back up our reasoning other than postulation … So I decided to do something about it and wrote up every way of initializing a singleton and inspected them at runtime using breakpoints. After analyzing each stack trace for any similarities I came across something interesting – PROOF!
There’s been a number of other good posts lately in the area of threading and memory safety; we recommend checking out
Atomics in Objective-C
I wanted to stop being unsure about the implications of typing one of those three words before variable / constant declarations, so I finally pulled up the Apple Documentation on ARC and began trying to digest the semantics. This article is an attempt to share what got sorted out in my brain as it regards ARC and Swift…
I often find myself worrying about retain cycles in my code. I feel like this is a common concern amongst others as well. I don’t know about you, but it seems like I am constantly hearing “When am I supposed to use weak? And what the hell is this ‘unowned’ crap?!” The issue we find is that we know to use strong, weak, and unowned specifiers in our swift code to avoid retain cycles, but we don’t quite know which specifier to use. Fortunately, I happen to know what they are AND when to use them!
Retain cycles in ARC are kind of like a Japanese B-horror movie. When you start as a Cocoa/Cocoa Touch developer, you don’t even bother about their existence. Then one day one of your apps starts crashing because of memory leaks and suddenly you become aware of them, and start seeing retain cycles like ghosts everywhere. As the years pass, you learn to live with them, detect them and avoid them… but the final scare is still there, looking for its chance to creep in.
Tip: Avoid retain cycles without doing the strong to weak dance by passing block parameters instead of remembering to capture weakly.
And Grand Central Dispatch helper frameworks in Swift seem to be quite a thing lately; here’s some for you to check out:
Async: “Syntactic sugar in Swift for asynchronous dispatches in Grand Central Dispatch.”
Eki: “is a framework to manage easily concurrency in your apps that wraps the powerful API Grand Central Dispatch”
GCDKit: “Grand Central Dispatch simplified with Swift.”
iDispatch: “Easy to use wrapper over GCD for iOS.”
KillerRabbit: “THGDispatch module, includes GCD bits such as Queues, Groups, Timer, Semaphore, etc.”
Throttle: “Lately I have been really interested in the AdvancedNSOperations talk given at WWDC last year. This talk and the awesome sample project released with it, kind of opened my eyes to the power of NSOperations..”