Drive Time with the Swift Standard Library

The last week and a half or so, I’ve been driving up and down the East Coast. (Hello I-95, my old friend. I’m paying tolls on you again.)

My trip included attending the 90th birthday of a family member in Virginia, visiting my sister in NJ, visiting a friend in Delaware, and then celebrating the birthday of my friend Larry Noto in Maryland. I also did an open mic night at Larry’s music store Music Land and saw Larry’s stand-up act , opening for Jon Lovitz.

While driving, I was thinking about a little piece of an app I’m working on. I write an integer out into a URL as a string and then read the integer back in when I interpret the URL—nothing earth-shattering.

My thoughts turned to ways to make that URL shorter and a bit less ugly, with the premise that longer URLs are uglier URLs.

As I made my way down the road, it occurred to me, a hexadecimal string would certainly be more compact. But then, if base 16 is good, base 32 would be even better. Two characters in base 32 could represent 1024 values instead of 100 in base 10. Three characters could represent over 32K values instead of 1000.

So, in the end I was looking for:

  • Small encoding. A way to encode an integer as a string that uses as few characters as possible.
  • A ‘URL-ready’ string. The resulting string should need no further URL encoding for things like spaces.
  • Easy to implement. The minor improvement is not worth a lot of engineering effort to implement or maintain.

Enter the Swift Standard Library

One drawback of a road trip is the inability to write code or even peruse documentation while driving. After arriving at my destination, I did some poking around and found these in the standard library:

An initializer for Int: init?(_:, radix:)

And one for String: init(_:, radix:, capitalized:)

In both initializers, the radix has a default value of 10 but can handle everything from base 2 to base 36.

Base 36?

In this base 36 encoding, the digits 0-9 and the letters a-z are used in the string representation. The decimal number 35 is z, the decimal number 940 is q4.*

Using a radix of 36 gooses the values representable by a two character string up to 1296, and by three characters up to over 46K.

And, as it turns out, I was already using those initializers with the default parameters to encode and decode the integer.

I ended up factoring out those calls into a few extensions on URL and URLComponents, but, conceptually the answer was simply to move from:

let stringValue = String(integerValue)
let int = Int(stringValue) ?? "0"

to

let stringValue = String(integerValue, radix: 36)
let int = Int(stringValue, radix: 36) ?? "0"

So, the solution met all of my criteria. I updated the code in my hotel room.

The Old Coldness

Checking on swiftdoc.org, it looks like this functionality has been in Swift since at least Swift 1.2.

This made me realize the even with all of the new things introduced with each release, Swift and the standard library had a lot of nice functionality from the beginning.

Some of my drive time thinking was about solving a programming problem. But then afterwards, my thoughts progressed to how we are understandably drawn to the brand new features, but that sometimes a piece of API that has been around for years can be brand new to you, and that’s just as exciting and delightful a discovery. •


* The encoding strategy in these standard library methods extend the encoding pattern used by hexadecimal numbers. Note that there are many different ways to encode digits or byte values, including Base32 described by RFC 4648.

WWDC in San Jose: Full Circle

Next week’s James Dempsey and the Breakpoints concert is especially meaningful to me. It circles back to the literal spot where it all began.

I’ve now been writing and performing these funny, nerdy songs about software development for sixteen years.

LIVE near WWDC 2017 Poster

It started in 2001. WWDC was held in San Jose and I was working at Apple. AirPods, iPhones, iPads, Apple TV, and Apple Watch did not exist. Event the first iPod would not be released until later that year. It was a different world.

The previous year, I had given my first WWDC presentation ever. The presentation went well, but speaking to a cavernous convention center room, it also felt a little dry.

While leaving the parking garage that night, a thought occurred to me: Wouldn’t it be funny if someone pulled out a guitar at the end of a session and played a little song to sum things up?

The thought stuck with me and over the next year I wrote a song1. The track manager thought it would be a fun way to close the session I was giving, so performing the song was a go.

I was expecting I’d be performing this song in a convention center room.

I could not have been more wrong.

That year, there were so many WWDC sessions that some were held across the street at the City National Civic theater. The CNC can seat thousands of people. Legends like Bob Dylan, The Rolling Stones, The Who and countless others have performed there.

As for me, I had never performed an original song for an audience before.

I had never even played guitar in public before.

Some people might have some friends come out to a coffee shop for an acoustic open mic night to make their performance debut. Somehow, I ended up making my debut in a 3,700 seat auditorium in front of a few hundred colleagues and developers.

I was a nervous wreck.

As I was wrapping up the session, I pulled out a guitar2 and told the audience, “I’ve never played guitar in front of an audience before, so it might just suck to be you.”

Fortunately the crowd kindly overlooked my musical errors and the song met with an enthusiastic response. From there, a song at WWDC became a yearly tradition until I left Apple in 2011. Since then, we’ve gotten to play more than one song in a row at the yearly LIVE near WWDC show. And it all started on the stage of that big hall in San Jose.

It is hard to convey how much it means to me to be performing at the City National Civic again.

Sixteen years ago, I faced my fears and stepped onto the stage alone to play a song as a humorous closing of a technical talk.

Next week, I’ll be returning to the exact same spot, backed by a full band, to perform a full concert of songs to raise money for a great cause.

If you are in town for WWDC week or if you live in the area, I invite you to come out for the show next Wednesday night, June 7th. No WWDC ticket is required, and it’s an all-ages show, so folks under 21 are welcome. The show is located directly across the street from the San Jose convention center.

The show is shaping up to be a lot of fun. I hope you can make it out to benefit App Camp For Girls and also to help celebrate this full-circle homecoming concert—for me it is very special and I’d love to see you there. •


James Dempsey and The Breakpoints, LIVE near WWDC
A Women@WWDC benefit for App Camp For Girls
presented by Capital One
Wednesday, June 7th, 7:00 – 11:00.
Event information and tickets

Can’t make the show but still want to support App Camp For Girls?
Click the Make A Donation button in the Buy Tickets section of the event page.


It was The Fetch Spec Song, about technologies now faded into to the mists of history: WebObjects and its object persistence framework, the Enterprise Objects Framework (aka EOF).

It wasn’t even my guitar, it was an indefinitely borrowed guitar