Swift: Using Grand Central Dispatch (GCD)

Grand Central Dispatch is a C API unlike other APIs that expose Objective-C classes. However, that doesn’t mean you can’t call GCD functions, or any other C API functions, directly from Swift. The C API functions are exposed to Swift just like they are in Objective-C. You can just call them like any other Swift function.

Here’s an example of a very common thing to do with GCD. Dispatch some code to the main thread asynchronously.

        dispatch_async(dispatch_get_main_queue(), {

            println(“I got dispatched!”)


The call looks very similar to what you do in Objective-C. The only different is that you don’t need the block (^) syntax required by Objective-C. You can even make it a little prettier in Swift by using the trailing closer syntax since the last argument is a closure.

        dispatch_async(dispatch_get_main_queue()) {

            println(“I got dispatched!”)




Swift: Selectors

Its very easy to use selectors in Swift. In Objective-C you would pass a selector to a method by using @selector(methodName). In Swift all you have to do is pass the method name as a string.


NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: “onTimer”, userInfo: nil, repeats: true)

This will call the onTimer function of self once per second.


Welcome to the New and Improved Software Logix Site!

I’m a software architect and developer with 25 years of experience from embedded to high performance server to UI and lately mobile development on iOS and Android.

You can get information about my Apps (OmniSketch, CubeScape, AcoustiGrid and Emotage) here.

I’ll be posting information on software development most likely focused on mobile application development on various platforms.