const app = {
lines: document.getElementById('lines'),
textbox: createTextbox(),
lineTemplate: createLineTemplate(),
}

const socket = connect(
`ws://${location.host}/app`,
{

initial({ lines, uuid, charLimit }) {
app.uuid = uuid
app.charLimit = charLimit
lines.forEach(addLine)
},

added(line) {
addLine(line)
},

removed(i) {
app.lines.children[i].remove()
},

server.onclose = (ws) => {
if (ws.line) {
const i = lines.indexOf(ws.line)
lines.splice(i, 1)
server.sendToAll({ removed: i })
}
}

server.commands = {

begin(ws) {
const line = {
hash: ws.hash,
uuid: ws.uuid,
text: '',
}

ws.line = line
lines.push(line)
server.sendToAll({ added: line })
},

run() {
console.log(`Running on port ${this.port}`)

this.wss = new WebSocket.Server({
port: this.port,
verifyClient: this.verify.bind(this)
})

setInterval(
this.prune.bind(this),
this.pruneInterval * 1000
)

this.wss.on(
'connection',
this.connection.bind(this)
)
}

Steven Degutis

Full-stack software developer for hire

Beginners Guide to Blocks in Cocoa

August 29, 2009 — 8 years ago
Note: This article was written 8 years ago. It may refer to files or websites lost to the ages. It may advocate outdated practices. Take it with a grain of salt.

Snow Leopard introduces Blocks, also called closures among other things, which are really really really cool. But, it's not immediately obvious from its name what it is, or how useful it can be, and when.

How it works

Basically, in laymen's terms, a block is a function that can be written directly within another function or method. And even cooler, blocks can be assigned to variables and passed as an argument to another function or method. A block can have a return value and arguments just like a function.

This means that you can write some code, and pass that code around to some other code, and let them execute the code when they're ready, with whatever arguments they want. Also, blocks can generally access the variables inside the first function (although, the rules to this feature are slightly more involved than I'm prepared to get into in the context of this post).

Technically speaking, a block is a first-class object type, like NSObject or NSString, and can be sent messages just like any other object in Objective-C. The two methods we will use quite often are -copy and -autorelease, which is explained at the bottom of this post.

When will we ever need it?

"But why would I ever need to write a function inside another function?!" you ask with the utmost urgency and frustration. Generally, there's 3 types of patterns in Cocoa that we run into often, where you'll really see blocks shine:

  • Notification-based APIs -- where you get notified when something can happen at arbitrary times in the future, and you'll want to do something at those times
  • Callback-style APIs -- async APIs such as sheets and alert panels, and other APIs that run async simply to keep the UI responsive (such as NSURLConnection and NSURLDownload) but generally "call you back" only once, on completion
  • Collection-iteration APIs -- including filtering dictionaries, sorting arrays, and performing some task on every object in a set, to name some examples

While these are good patterns, they often come with a lot of baggage... For example, the method names are oftn very long, and you're usually sending context information which you're in charge of memory-managing. Not to mention, separation of the logic of one task into two places is really uncool, and sometimes makes it harder to debug things since you're constantly looking in two different places (at least!).

Using blocks to make life simpler

For notification-based APIs, such as Key-Value Observing and Notifications, and Callback-style APIs such as NSAlert, we can now write our code inline. So, instead of passing a target/selector (and often context) to get notified when changes occur in the API, we can write the handler right inline with our registration code, thus greatly simplifying our code and making debugging straightforward.

Collection-iteration APIs now make life noticably simpler as well. Imagine you have a collection of friends. Now, if you wanted to filter the collection based on sex, hair color, and height, you'll generally need to create a new mutable collection, loop through the original one, make 3 comparisons and if they all match, add it into the new mutable collection. (Optionally you might want to return an immutable version of the collection from a method or function.) Instead, we can now pass a block to -[NSArray indexesOfObjectsPassingTest:] and we get back the indices of all objects which match the test. (To be fair, this is not the most exciting use of blocks.)

Real-world example

Let's take a look at a concrete example here using NSAlert. This is typically how you would use an NSAlert in your code:

- (IBAction) doSomething:(id)sender
{
    NSAlert *alert = ...
    [alert beginSheetModalForWindow:myWindow
                      modalDelegate:self
                     didEndSelector:@selector(myDoingSomethingAlertDidEnd:returnCode:contextInfo:)
                        contextInfo:myContextInfo];
}

- (void) myDoingSomethingAlertDidEnd:(NSAlert *)alert
                          returnCode:(NSInteger)returnCode
                         contextInfo:(void *)contextInfo
{
    if ([self checkContext:contextInfo]) {
        switch (returnCode) {
            case NSAlertFirstButtonReturn:
                [self doSomething];
                break;
            default:
                break;
        }
    }
}

That's pretty ugly, we've created a whole new method on our controller class just to handle the results of an alert. (I've rarely used the contextInfo parameter, but it can be useful.) So let's look at the same concept using a blocks-based API:

- (IBAction) doSomething:(id)sender
{
    NSAlert *alert = ...
    [alert beginSheetModalForWindow:myWindow
                  completionHandler:^(NSInteger returnCode) {
                      switch (returnCode) {
                          case NSAlertFirstButtonReturn:
                              [self doSomething];
                              break;
                          default:
                              break;
                      }
                  }
     ];
}

The difference is all in where the code is handled, and how much boilerplate we've eliminated just to perform this simple asynchronous task. In this second version, the code that is supposed to run after the alert is handled, is written at the exact same time as running the method, which makes things much clearer.

Another real-world example of how blocks makes things much simpler, is in KVO observing, which Jon Wight recently posted about with a really cool solution. If you've ever dealt with KVO before, you'll see how his sample code can save you a ton of headache and hassle.

(Technically blocks could be set to a variable and passed to the method like anything else, since blocks are Objective-C objects as mentioned earlier. While that's a little redundant in this example, assigning blocks to variables can often be necessary or useful in refactoring or slimming down your code.)

Some tips...

Blocks do not follow the same retain-counting rules as typical NSObjects, and instead follow stack deallocation rules, just like primitive types (int, long, and float, for example). Thus, when returning blocks from methods, they should be sent -copy (and -autorelease if you aren't Garbage Collected) to ensure their existence.

Many APIs in Cocoa and Foundation make use of blocks in 10.6, such as NSOperationQueue, NSArray, and plenty more, so take a look around at the release notes to check out what you can do. Specifically, look at the new libdispatch functions such as dispatch_async and the like. (Don't be afraid to use C-based functional APIs in your Cocoa code when needed, they really aren't scary!)

While variables from the enclosing function/method can be accessed within a block, there are some tricks to doing this properly, including the occassional necessity of the keyword __block, which is explained much better in Mike Ash's post on practical blocks. (He also has some good tips about style when using blocks, in his post.)

While many new APIs make use of blocks in 10.6, not all of them do just yet. Many which could use it (such as NSAlert) are missing this cool functionality, so if you see a need, don't be afraid to fill it and submit it to github (or some similar site)! And also please submit it as a Radar, because it would rock if 10.7 gave us new blocks-based APIs.

About me

My name is Steven Degutis, and I've been writing software professionally for almost a decade. During that time, I've written many apps and websites, quite a few techical articles, and kept up-to-date with the rapidly evolving software industry.

If you have software needs for web, mobile, or desktop, and are looking for a seasoned software professional, please reach out to me at sbdegutis@gmail.com to set up a phone call.

Work Experience

  • Self-employed – present
  • Clean Coders – 5 years
  • 8th Light – 2 years
  • Big Nerd Ranch – 1 year
  • Self-employed - 1 year

Platforms

  • Web: full-stack
  • iOS (UIKit)
  • macOS (Cocoa)
  • REST APIs
  • AWS / EC2 / ELB

Languages

  • JavaScript
  • HTML5 / CSS
  • Swift
  • Objective-C
  • Clojure

Frameworks

  • Node.js
  • Express.js
  • React
  • Vue.js
  • Electron

Technical articles

Over the past decade, I've written a total of 169 technical articles on various programming languages, frameworks, best practices, and my own projects, as I kept up-to-date and active in the software industry.

Subscribe via RSS / Atom.

Chronological

Portfolio

Here are some of the projects I'm most proud of. They were created using a variety of technologies, running on several different platforms and OSes. They're all finished products, and many of them are open source.

CleanCoders.com

Website - Online Video Store

I wrote this web store for Robert "Uncle Bob" Martin, using Clojure for the back-end, and JavaScript for the front-end. Over the course of 5 years, I took the site from a simple three-page website to a full enterprise-ready business solution, with nearly 100% test coverage.

  • Clojure
  • Datomic
  • jQuery / D3.js
  • JavaScript
  • ClojureScript

Docks

macOS app - Dock Utility

I made Docks in 2009 for users who wanted to swap out icons in their Dock with a single click. Its unique functionality and design aesthetic attracted the attention of Apple, Engadget, MacWorld, and led to an acquisition of my start-up by Big Nerd Ranch.

  • Apple.com Staff Pick
  • MacWorld 4/5 Rating
  • MacWorld Gem of the Year
  • Featured on Engadget.com

Leviathan

macOS app - Clojure IDE

Source Code

While working on CleanCoders.com, a website written completely in Clojure, I increased my productivity by building a custom IDE for macOS designed specifically for Clojure projects.

  • Objective-C
  • Clojure
  • C / C++
  • Cocoa
  • Themeable

Zephyros

macOS app - Hackable Automation

Source Code

This began as an experiment to see how many languages I could use to script a custom macOS window manager using our custom TCP protocol. Eventually it had bindings for Clojure, Ruby, Python, Go, JavaScript, CoffeeScript, Node.js, Chicken Sceme, and Racket, as well as other community additions.

  • TCP / Unix sockets
  • Custom protocol
  • Highly Scriptable
  • 10+ language bindings
  • Open source community

Bubble Maker

iOS app - Bubble simulator

This toy was made in a weekend to entertain my 1 year old daughter. It lets you create bubbles with your fingers, which then simulate physics by bumping into each other and falling.

  • SpirteKit
  • Custom art
  • Physics simulation
  • iOS
  • tvOS

Quick List

iOS app - Todo list app

When I couldn't find an app in the App Store that let me make very simple lists extremely quickly, I made one myself. I use it almost every day to organize and track my activities.

  • In-app purchases
  • Custom UI / UX
  • Social media
  • App Store artwork
  • Spring animations

sdegutis.com

Website - Personal Portfolio

Source Code

This very site itself was written from scratch in about a day. It uses best practices for modern responsive web design, and a custom build phase to compile the sources into a single HTML file.

  • Node.js
  • Pug / Jade
  • LessCSS
  • HTML5
  • WebSockets

2048

Java app - Game

Source Code

The game 2048 (created by Gabriele Cirulli) is so fun that my kids wanted their own copy. So I wrote this version in Java 8, using JavaFx for attractive graphics and silky smooth animations.

  • Java 8
  • JavaFx
  • Modular code
  • Customizable
  • Animations

Mjolnir

macOS app - Window Manager

Source Code

I created this app to increase my productivity by letting me move windows around in macOS using keyboard shortcuts. It grew into a community-driven highly extensible app, using Lua for its plugin system.

  • Objective-C
  • Embedded Lua
  • Plugin system
  • Fully documented
  • 5,000 GitHub stars

AffluentConfidante.com

Website - Social Network

Implementing this elite social network gave me experience integrating both Apple Pay and credit card payments (via Stripe.com) seamlessly into web apps, for a frictionless and pain-free payment experience.

  • Clojure
  • Elastic Beanstalk
  • PostgreSQL
  • Stripe.com
  • Apple Pay

HyperChat

Website - Live Chatroom

Source Code

This isn't just any chatroom. In this web app, you can see what everyone is typing while they type it. I made this in order to scratch my itch for making real-time apps and games, and learned how to use WebSockets in the process.

  • JavaScript
  • WebSockets
  • Node.js
  • Vue.js
  • CSS

Bahamut

macOS app - Music Player

Source Code

As iTunes went through many user interface changes, I wanted an app that was consistent, intuitive, and easy to use. So I created Bahamut, a minimal music player for macOS with a custom user interface.

  • Objective-C
  • Custom UI
  • Cocoa
  • Core Data
  • AVFoundation

Chatter

macOS app - Chat (IRC) Client

Source Code

This was written in 2009, before the time of Slack, when IRC was the main way for programmers to get short-term assistance from each other. Its purpose was to be a beautiful app with an emphasis on simplicity and usability over technical power.

  • Async networking
  • Core Animation
  • Core Text
  • IRC Protocol
  • UI Design

AppGrid

macOS app - Window Manager

Source Code

This is an app I actually use every single day. It lets you move windows with global keyboard shortcuts. Since it uses Vim-like key bindings, it should feel pretty natural to any programmer. There's no configuration needed; it Just Works™.

  • Minimalist UI
  • Simple UI
  • Vim-like Hotkeys
  • Global Hotkeys
  • Zero-configuration

Hydra

macOS app - Lua window manager

Source Code

As an evolution of Phoenix, Hydra was my first attempt at embedding a full Lua virtual machine into an Objective-C app, to make a lightweight and efficient window manager that focused on speed, low memory usage, low CPU usage, and overall being gentle on laptop batteries.

  • Embedded Lua
  • Generated docs
  • Lightweight
  • Memory efficient
  • CPU efficient

Phoenix

macOS app - JavaScript window manager

Source Code

As an evolution of Zephyros, Phoenix was my attempt to use Cocoa's native JavaScript bindings to make a more lightweight and efficient window manager, that focused on speed, low memory usage, low CPU usage, and overall being gentle on laptop batteries.

  • JavaScriptCore
  • JavaScript API
  • Lightweight
  • Memory efficient
  • CPU efficient

Smaller projects

These may be tiny, but they're interesting technical feats.

Lua4SwiftSwift framework for embedding Lua with a native Swift API.
chooseCommand line fuzzy-matching tool for macOS that uses a GUI
musicCommand line music daemon for macOS that only speaks JSON
hectoCommand line text editor with an embedded Lua plugin system
ZephSharpWindow manager for Windows using Clojure for scripting
managementMinimalist EC2 configuration & deployment tool in Ruby.
go.assertAssertion helper package for writing tests in Go.
go.shattrGo library for printing shell-attributed strings to stdout.
OCDSpec2Objective-C based testing framework with Xcode integration.