Amro Mousa

iOS developer, BMW fan, and amateur photographer.

Page 2

More on Lazy Initialization in Swift

@ColinEberhardt wrote a great post titled Swift Initialization and the Pain of Optionals. In the post, Colin mentions not being able to pass parameters to initializers when assigning to a variable marked lazy. The workaround he found was to use a closure. Colin gave two examples:

lazy private var animator = UIDynamicAnimator(referenceView: self.view)

which did not compile and:

lazy private var animator: UIDynamicAnimator = {
    return UIDynamicAnimator(referenceView: self.view)

which did compile. @schukin noticed that Colin also added an explicit type to the variable declaration in the second example. Doing the same with Colin’s first example fixes the compilation error:

lazy private var animator: UIDynamicAnimator = UIDynamicAnimator(referenceView: self.view)

So it turns out that you can reference variables regardless of whether or not you use a closure. The important...

Continue reading →

GCD: Using Dispatch Groups for Fun and Profit

I’m republishing this article here so I can reference it later.

I recently needed a way to perform an unknown number of asynchronous http requests and wait until they were all done before proceeding. dispatch_groups are a neat feature of Grand Central Dispatch (GCD) that made this easy to do.

There are a couple of ways to use dispatch_groups but the basic idea is the same: create a dispatch_group, give it some tasks, and wait for it to finish those tasks.

First, let’s create a group:

dispatch_group_t group = dispatch_group_create();

There are two ways to add tasks to our dispatch_group. You can either call dispatch_group_async with the group, a dispatch_queue and block to run or manually call dispatch_group_enter.

Calling dispatch_group_async looks something like this:

dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    // Do stuff on

Continue reading →

Jarek’s Anecdote

I’ve just started reading Ed Catmull’s book, Creativity, Inc. Early on Ed explains that he came up with both the z-buffer and texture mapping techniques that had a profound impact on my childhood (Threewave CTF in Quake 1, anybody?). This reminded me of an anecdote the instructor of my second graphics class once shared.

Jarek Rossignac told us about his giving a talk during his tenure as a researcher at IBM. Jarek’s talk was about his research of subdivision of curves. After the talk, a gentleman struck up a conversation with him. That gentleman ended up being Pierre Bézier, the man who popularized the Bézier Curve.

Continue reading →