Avoid using class names in Swift’s #selector

Don’t use class names in Swift 2.2’s new #selector. In other words, don’t do this:

Here’s the problem: it compiles, so you’d expect it to work, right? Isn’t that what #selector is all about? But let’s say you decided to refactor this code out of MyViewController and into a child view controller called MySecondViewController. (This just happened to me, …

Tiny Swift Tip: Initializing Empty Arrays

If you’re initializing your empty arrays by specifying the type annotation like this… Hold on — what’s a type annotation? Great question. You use them all the time when you define methods that have at least one parameter:

The : Int is the type annotation for the id parameter. A type annotation starts with a colon and ends with …

How to handle null JSON values in Swift

A Parsing JSON in Swift reader recently wrote in and asked: I did have a question regarding getting a null value in your JSON. I know null is different than a nil value so I didn’t know how to go about safely receiving it? So what do you do if you get a null value in your JSON? How do …

Debunking the myths about parsing JSON in Swift

There are a bunch of myths out there about parsing JSON in Swift. And the problem with these myths is that they lead us to solve imaginary problems using bloated frameworks or wacky techniques that gain us very little, especially considering the cost they impose on us. Here are a few of the most popular myths — and the truth …

Year in Review 2015: The Year of Parsing JSON in Swift

I enjoy reflecting and writing these articles each year, and while I write them for myself, others have told me they enjoy reading them. If you missed previous years, you can find them here: 2014: What I learned about building an audience, selling products, and productized consulting 2013: What I learned from launching one failed and one successful product (and …

Reducing clutter in your JSON parser through proper typing

This article is an excerpt from Parsing JSON in Swift. When you’re casting types as you parse your JSON, be as specific as you can to reduce clutter. This is especially important when working with arrays, which generally contain just one type. Let’s look at how we might parse the following JSON (from the GitHub Search Repositories API):

For …

How to test your JSON parser

Earlier, we discussed why you should test your JSON parser and how it can save time, catch bugs, and help you build confidence that your code does what you want. But how do you write tests for your JSON parser? And should you make network requests to the API in your tests? Let’s answer the second question first. Should you …

Why test your JSON parser?

One of the big benefits to creating a separate JSON parser class is that we can write automated tests for it easily. But why would you want to write tests? What benefit could they possibly provide? Unit tests provide at least five main benefits: they help you ensure correctness during development, catch bugs in existing features, allow you to refactor …

APOD App Rejection: Postmortem

My heart sank when I saw the little red dot in iTunes Connect. Apple had just rejected my app. I spent a good portion of my free time during the previous five weeks building my app for the new Apple TV and submitted it just ten hours after they opened submissions. (If I hadn’t been sleeping, I would’ve submitted it …

How to parse JSON with Swift 2

I’ve written about how to parse JSON in Swift before, but someone asked recently about how to do it with Swift 2 and I realized that everything changed from Swift 1.2 to Swift 2.0. So how do you parse JSON with Swift 2? We’ll look at how to parse a JSON array that we could use to display a list …