Objective-C’s Designated Secret

For the past several iterations of Xcode, Apple has quietly but steadily improved the quality of the backing toolchain. In particular, the clang static analyzer has gotten quite a few improvements; however, LLVM hasn’t been neglected.

Far from it. In Xcode 5.1, Apple shipped an updated version of LLVM that included upstream revision 196314. The summary of that change is brief — “Introduce attribute objc_designated_initializer” — but the potential implications are wide-ranging. But first: what are they even talking about?

Read on →

Objective-C Debugging Cheat Sheet

Over the years, Apple has provided a few handy tricks here and there about debugging Mac and iOS apps. Sometimes, these tricks take the form of private methods implemented by framework classes; if you know the right selector, you can get your objects to dump all kind of debugging information. (Of course, none of these selectors are suitable for use in released apps, but they can prove really handy during the development cycle.)

Since none of these are really documented, though, it’s easy to confuse which private methods are available on which classes. For example: if I have a UIView, should I use -_subtreeDescription or -recursiveDescription to log out its subview hierarchy? And is that really where that underscore goes, or did I get it backwards?

After some nudging from coworkers, I took some time and scraped together all the various private methods I could find (as well as a few suggested on Twitter) and combined them into one debugging cheat sheet, which I’m making available right here. Download it today, and suggest more!

Update: @monowerker put together a Dash version of this cheat sheet, which you can find on GitHub here. Thanks!

Learning Swift: Optional Types

Note: this post is part of a series about the Swift programming language, introduced at WWDC 2014. I’m no more experienced in Swift than anyone else outside Apple, but I learn best by coding and talking through a problem. If there’s a better way to approach some of these topics, get in touch on Twitter!

We’ve had a little over two weeks to play with the Swift programming language now, and one sharp edge that keeps coming up is the language’s inclusion of what they call “optional types”. The vast majority of Objective-C developers are familiar with the use of nil to indicate a nonexistent value, but communicating that kind of information through a variable’s type is a bit more foreign.

In this post, we’ll have an introductory discussion about how Swift provides optional types, go over a couple of implementation details, and point out a few tough spots in the optional system.

Read on →

Learning Swift: Ordered Dictionaries

Note: this post begins a new series about the Swift programming language, introduced at WWDC 2014. I’m no more experienced in Swift than anyone else outside Apple, but I learn best by coding and talking through a problem. If there’s a better way to approach some of these topics, get in touch on Twitter!

An ordered dictionary is a useful data structure that combines attributes of an array and a dictionary. Like an array, it stores elements in the order they’re added, and enumeration happens in the same fixed order every time. It also satisfies the basic idea of a dictionary: it stores key-value pairs, and can look up values by key.

Ordered dictionaries are incredibly useful tools for all kinds of development. To crib an example from the Swift book, an ordered dictionary might help an app display people and their ages: you might use names for keys, ages for values, and use the order to provide data in table cells by index path (and support user-driven reordering, to boot).

In this article, we’ll build an ordered dictionary atop the two primitive collection types already in Swift: an Array and a Dictionary. Let’s go!

Read on →

Xcoders Talk: UICollectionView

I recently had the opportunity to give a talk at Seattle Xcoders about UICollectionViews in iOS 6. Unfortunately, I didn’t get a recording or screencast, but the slides are available here; you can also find the source code used in the talk (spruced up a bit) over at GitHub.

AppleScripting Keynote PDF Export

This past weekend I found myself in a situation where I would need to export 20 or so Keynote files to PDF. These generated PDFs all needed to have the same export options (no added dates, no presenter notes, etc.), and I really wanted to avoid having to go through all of them manually, clicking all the relevant boxes – which Keynote helpfully resets every time you hit Export.

Thankfully, there’s a (slightly) better way: AppleScript. With a couple hours’ work, I was able to tweak this script to work with the latest version of Keynote (5.3 at time of writing). The results aren’t great – the exported file always ends up in your Documents folder, and I don’t quite trust some of the delay statements – but it gets the job done with little more than a drag & drop.

Read on →

Audible Fun With Alfred

I’ve been looking more and more at using AppleScripts in Alfred, my launcher/productivity app for Mac OS X. Today’s snippet is about switching the active audio output device; I wanted this for work, where I switch between external speakers and some headphones plugged into the front 3.5mm jack.

Read on →

Bukkit Can Hold Scala!

Scala has been a fairly interesting language to me ever since dealing with it a few years ago for a paper on functional programming; when I saw it referenced again recently (in an article about D, of all things), I decided to give it a try. Scala is notable for being runnable on the JVM, with the right libraries – and what better way could there be to learn a JVM-compatible language than to write a Bukkit plugin with it?

Read on →

Alfred, You’re a Genius

Alfred is one of my personal favorite and most-used apps on my Mac, and with the PowerPack, it’s become my primary portal into iTunes as well. (Those of you with Macs but without Alfred are missing out.) However, there remains one big deficiency in Alfred as it ships: there’s no ability to generate a Genius playlist from the currently playing song.

Read on →