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.

A Generic Swift Blog Post

I began investigating SwiftUI to see if it is indeed the shortest path to a great app. Since then, I committed an update to the MemeMaker project that makes the initial interface a list of examples. I was experimenting with passing a different View into each row as the destination of the row’s NavigationButton, rather than hard-coding the destination.

I found that you can’t just make a property of type View. I believe that is because it uses Self within its declaration of the body property. And so the compiler dutifully complained.

Seeing the type-erasing AnyView type in the documentation, I wrapped everything in AnyViews to get it all working.

I got some feedback from Matt Ricketson on Twitter suggesting I use generics here instead.

Of course it makes complete sense, with better code and less code. You can see the diff here.

With the SwiftUI and Combine frameworks using generics so heavily, I need to develop my intuition about generics beyond arrays and dictionaries. I also really need get better at reading declarations that are chock full of generics.

Fortunately, I think the two go hand in hand.

When Swift first arrived, I found reading optionals in code to be difficult. But AppKit and UIKit development meant using them immediately. I dug in to understand them as best I could. I even wrote a song about it. And now reading and writing code with question marks and exclamation points seems natural, not baffling.

I’m hopeful that as I use SwiftUI and Combine that I’ll refine how I think about generics as well.

I’m also curious. Are there ways that you think about genetics that have made them more understandable for you? (Not so much the syntax, more so the zen of generics) Any articles that you’ve found particularly useful?

I’d love to hear. Please send any of your generic thoughts or thoughts on genetics to @jamesdempsey at Micro.blog or Twitter.

Also, if you are ever writing about generics on an iPhone: BEWARE! Autocorrect really wants to replace ‘generics’ with ‘genetics’! •

SwiftUI: Almost Too Much Fun

In my previous post, I described the JDBP band app I want to build and the technologies introduced at WWDC 2019 that I think might make it much easier to create.

Part of the process is exploring these new technologies and SwiftUI is first on my list.

As I’m playing with SwiftUI, I’m not thinking too much about the app I want to build. For me, and probably many others, my first step beyond watching videos and walking through tutorials is to play with it, use it to build a few small throwaway apps, and try to develop as good a feel for it as I can in a short period of time.

I’m not at a point where I can nitpick over whether I agree or disagree with this or that design choice in the framework. I definitely don’t know it well enough yet.

Currently, I’m in the process of building up my mental model of how all of this works. This also includes mentally diffing that model with my existing mental models of AppKit and UIKit.

That’s a lot of brain cycles. This is going to take a while and is probably also going on as a background process as I sleep.

I found these two sessions really helped me begin to build that mental model of the frameworks beyond tutorials and demos:

I am sure I will watch them multiple times. I also hope we get more documentation, more tutorials, and maybe even more WWDC-like videos from Apple. I feel like what we saw in sessions at WWDC and in the tutorials only scratch the surface of SwiftUI.

How Does It Feel?

I don’t have enough experience with the framework yet to declare that SwiftUI is the shortest path to great apps.

But I’m sure having a blast playing with it!

I haven’t felt a sense of joy and exuberance like this about UI programming for a very long time.

Will that sense of joy fade as I start trying to use SwiftUI for a ‘real app’?

Certainly the excitement of novelty will fade as it becomes more familiar. For other aspects, time will tell, but for now I’m just going to enjoy the ride.

Joy

For me, there’s joy is in how quickly you can compose a complex view—even a live data-driven view—without much code at all. There’s joy in the tooling and preview mechanism, which is flexible and fairly responsive.

And there’s joy in the fact that the views that you compose are well-behaved.

All those things that may have caused you to breathe a tired sigh in the past—things you knew you should or must support, like Dynamic Type, Right-to-Left UIs, Dark Mode and more—now happen automatically and correctly.*

The framework and tools seem to be very thoughtfully designed, in terms of the functionality that is provided, but also in the unified way that things fit together, scaling from structural components like navigation to views and controls, all the way down to shapes, paths, and graphics operations.

MemeMaker

Yesterday, I posted a meme that seems to have gotten a few amused smiles:

Meme of Scarlett O'Hara with words "As God is my Witness, I'll never type translatesAutoresizingMaskIntoConstraints again"
I’ve been playing with SwiftUI and have a declaration to make.

I used SwiftUI to compose the meme and have posted the MemeMaker project on GitHub.

Meme Maker is a not-particularly-functional app created by someone on their couple of days using a new technology:

  • You can’t edit the text except by editing the code
  • You can’t different image unless add other images to the project
  • You can’t export or share the meme

The app composes some text over an image in a few different ways and then you can take a screenshot of the result to share if you want to.

But it also shows a few things people might find interesting:

  • The main list uses AnyView to pass destination view into row
  • The Overlay example uses the overlay modifier to overlay the text on top of the image. Probably the cleanest solution
  • The ZStack example shows my original example using nested zstacks to pin text to top and bottom of image
  • The Text Gone Wild example shows what happens when the text is allowed to spread out as far as it can
  • All examples show using a named font instead of a semantic font
  • All examples show adding a shadow to text and using a non-named color

So, I put it out there if you want to check it out.

Feel free to download it or fork it and use it as a starting point for your own explorations. If you discover alternate ways to achieve the same result or any other cool things, let me know at @jamesdempsey at Micro.blog or Twitter. •

Feedback

I’m also trying to post a record of feedback I’ve filed. If anything sounds good to you feel free to file similar feedback.

Feedback Filed:

  • FB6145912 Would love an ‘Embed In…’ menu option when writing SwiftUI code
  • FB6145990 Would be great to have option to refactor a View directly into a new SwiftUI file
  • FB6139401 Framework Reference docs should include a Symbol Index Oh how I miss having that big, high info density index of Class / Struct / Protocol / Function symbols!

*And you didn’t breathe that heavy sigh because you didn’t want your app to have that functionality—it was because it takes a chunk of time and work to do and do correctly—time you wish you could spend improving the core functionality of your app or maybe even spending with family and friends.

The Shortest Path To A Great App

The Shortest Path To A Great App” That is the phrase Apple used in a number of the SwiftUI sessions at WWDC last week. But beyond SwiftUI, there were a number of other announcements at WWDC 2019 that also could lead to a shorter path to a great app.

Taken in total, these announcements have left me feeling incredibly energized.

An Oddball WWDC Week

I have an oddball WWDC week compared to most people attending WWDC, AltConf, or Layers.

For the last eight years, I’ve organized and performed in the annual LIVE near WWDC concert on Wednesday night. The last four events have been benefit shows for App Camp For Girls.

I’ve also had the honor the past few years of closing out AltConf on Thursday afternoon with a lighthearted musical Week In Review session. Of course, I have no idea what I’ll be talking about until after Monday’s announcements.

Preparing for these events keeps me pretty busy until late Thursday afternoon—with a large chunk of Monday set aside for the Keynote and Platforms State of the Union—and then viewing sessions or snippets of sessions as I can.

So Friday is the first day of WWDC week I can dig deeper into everything that has been announced.

An Aha! Moment Before Breakfast

It dawned on me early Friday morning that a set of announcements at WWDC 2019 should make an app I have wanted to build much easier and quicker to create.

The annual LIVE near WWDC show is a complicated undertaking. This past year we had 24 performers playing 18 different instruments on 18 different songs.

In addition, for the past few years, Adam Tow has taken some really fantastic photos of the show.

So, for a long time I’ve wanted to build an app for my band, James Dempsey and the Breakpoints (JDBP). The app would be a place to share photos, lyrics and info with fans, and also be a resource for people performing in the show—the Breakpoints.

Fans would be able to download the free app and without an account, see photos, browse photos of past performances, see lyrics, maybe even play JDBP songs from Apple Music if they are subscribed.

Breakpoints would be able to sign in, see info about the upcoming show they are performing in, view the (ever changing) set list, know which songs they are playing on, and be able to swipe through the chord charts for the songs they are playing on, in order on an iPad (or iPhone in a pinch).

But, until last week, just thinking about building that app was exhausting. It would be just me writing it and would need a lot of infrastructure code to get it right.

Then, Friday morning, the thought occurred to me that these three newly announced technologies could make writing this app significantly faster and easier:

  • SwiftUI
  • Sign in with Apple
  • Core Data / CloudKit interoperability

A Journey of Discovery

Will these three newly announced technologies do the trick? What dead ends, limitations, and blind alleys will I discover?

At the moment I have no idea, and that’s exciting.

I still have videos to watch, documentation to read, and lots of code to write and fiddle around with.

The app I’m building won’t be open source, but I’m hoping to make it an ‘open process’ app. My plan is to write little blog posts as I go.

These posts will be me thinking out loud — expressing what I want this app to do before I’ve even fully investigated how I will implement it. And then reporting what I find as I investigate things and see how they work in practice. The intent is for this to be a public journal or technology travelogue as I go through the process of building an app.

I’m also hoping the posts will help spur discussion and feedback and help us all come to understand these technologies better.

Thanks Brent!

This approach was greatly inspired by Brent Simmons. I’ve always admired Brent’s ability to post in the casual and open manner of “Hey, I’m working on a thing. I’m learning it and I’m not an expert. Here’s what I’m discovering and here’s what is confusing to me.” Brent has been a prolific developer for many years, but he never claims to have all the answers. (Plus he plays a mean keyboard.)

A Last Caveat

This app is a side project. As the glow of WWDC 2019 fades and day to day tasks come back to the fore, there may stretches of time between posts about this app. I appreciate your patience in advance.

Let’s see how short that path really is.

I’m so excited to get started! •

Where I’ll be at WWDC 2019

I’ll be doing a few things next week at WWDC 2019 and wanted to pass along my schedule. Hope to see you at one or more of these happenings!

Tuesday: Micro.blog Meetup

Micro.blog Logo

Tuesday at lunch time, I’ll be at the Micro.blog meetup at the Grace Deli, across from The Hilton.

My good friend—and meetup organizer—Jean MacDonald and I co-host The Weekly Review, a ‘microcast’ podcast about personal productivity hosted on Micro.blog.

Stop by and say hello! We’ll also have The Weekly Review stickers to hand out. You can RSVP here.

Micro.blog Meetup, June 4th, 12 – 2 pm

Wednesday: LIVE near WWDC 2019

James Dempsey and the Breakpoints JDBP Logo
Wednesday night, my band, James Dempsey and the Breakpoints, performs our 8th annual LIVE near WWDC show. The party starts at 7 pm—tickets include an open bar. The band goes on at about 8:30 pm, playing a full concert of original, humorous, programming-oriented, and yes, even chart-topping music.

But the night is more than just music and fun—it’s a benefit concert for App Camp For Girls, with ticket proceeds going to support their mission. Come on out on Wednesday night to a great event for a great cause!

Tickets are still available but going fast. Click through to get all the details and buy tickets.

James Dempsey and the Breakpoints, LIVE near WWDC, presented by Capital One
Wednesday, June 5th, Doors open at 7:00 PM, show starts at 8:30 PM. Party until 11:00 PM

Thursday: AltConf WWDC 2019 Week In Review

AltConf Conference Logo
I’ll be closing out the week at AltConf with my sixth annual WWDC Week In Review, a lighthearted look back at the announcements and events of the week.

After a week front-loaded with announcements and technical talks—this easy-going closing session of AltConf combines humor, music, and perspective.

Join me for what usually proves to be an entertaining and occasionally enlightening session.

And, as usual, there will be a bit involving a ukulele.

AltConf 2019, June 3 – June 6, 2019
Closing Session, Thursday, June 6th, 3:00 PM

A Busy WWDC 2019 Week + Stickers!

As usual, it will be a busy WWDC week with plenty of new announcements and technology to wrap our heads around. If you see me out and about during the week, please say hello!

I’ll also have brand new James Dempsey and the Breakpoints and The Weekly Review stickers to hand out. Just ask for a sticker or two when you say hello!

I hope you can make it out to one or more of these events—see you at WWDC! •


Where I’ll be at WWDC 2018

I’ll be doing a few things next week at WWDC and wanted to pass along my schedule. Hope to see you at one or more of these happenings!

Tuesday: Speaking at the NextDoor Conference

Logo for the Next Door Conference

Tuesday morning, I’m speaking at the Next Door conference, located, remarkably enough, next door to WWDC.

I’ll be talking about two of the more glamorous aspects of app development: build settings and xcconfig files. In the session, we’ll cover the why and how of giving your build settings an out-of-project file experience to make build settings easier to manage.

Registration is open until Monday and a there’s a great lineup of speakers. I encourage you to check the conference out.

NextDoor Conference, June 4th – June 7th
My session about xcconfig files: Tuesday, June 5th, 10:00 AM

Wednesday: LIVE near WWDC 2018

James Dempsey and the Breakpoints JDBP Logo
Wednesday night, my band, James Dempsey and the Breakpoints, performs our 7th annual LIVE near WWDC show. The party starts at 7 pm—tickets include an open bar. The band goes on at about 8:30 pm, playing a full concert of original, humorous, programming-oriented, and yes, even chart-topping music.

But the night is more than just music and fun—it’s a benefit concert for App Camp For Girls, with ticket proceeds going to support their programs. Come on out on Wednesday night to a great event for a great cause!

Tickets are still available but going fast. Click through to get all the details and buy tickets.

James Dempsey and the Breakpoints, LIVE near WWDC, presented by Capital One
Wednesday, June 6th, Doors open at 7:00 PM, show starts at 8:30 PM. Party until 11:00 PM

Thursday: AltConf WWDC 2018 Week In Review

AltConf Conference Logo
I’ll be closing out the week at AltConf with my fifth annual WWDC Week In Review, a lighthearted look back at the announcements and events of the week.

After a week front-loaded with announcements and technical talks—this easy-going closing session of AltConf combines humor, music, and a bit of perspective. I hope you join me to be entertained and maybe occasionally enlightened.

And, as usual, there will be a bit involving a ukulele.

AltConf 2018, June 4 – June 7, 2018
Closing Session, Thursday, June 7th, 3:00 PM, Room 1

A Busy WWDC 2018 Week

As usual, it will be a busy WWDC week with plenty of new announcements and technology to wrap our heads around. If you see me out and about during the week, please say hello!

I hope you can make it out to one or more of these events—see you at WWDC! •


Introducing Backtrace

Album Art for James Dempsey and the Breakpoints Album 'Backtrace'

We are beyond thrilled to announce the release of Backtrace,
the debut album from James Dempsey and the Breakpoints.

It feels great for this to be finally out in the world.

We hope you love every track.

And if you do, please don’t keep it to yourself.

We hope you spread the word.

(tweet, facebook, review and blog like the wind!)

Now available for download worldwide via iTunes

Download on iTunes