3 lessons I learned from calling an API in Swift

After finishing the Flatiron School’s free course on Swift, I decided to solidify my Swift skills by building an Italian dictionary iPhone app. I thought this would be a fairly straightforward project to give myself some additional practice. I already knew a great language translation API, and I’ve called JavaScript APIs before. I assumed that Swift would be no different.

I was wrong. Here are three things I learned from my struggle to call an API in Swift (and parse the JSON):

1. Calling APIs with Swift is a lot more complicated than using jQuery.

After calling an API in Swift and recently calling one in pure JavaScript as well, I can’t believe how easy jQuery makes it. Thank you, jQuery! However, internet tutorials help a lot when calling APIs. What I tend to do in any API-calling situation is Google the code for it and then put in my own URL (and troubleshoot accordingly). Swift posed an additional challenge because the versions of the language have changed so quickly and so recently, but I adapted the code from this tutorial to make a successful API call.

I recommend having a good grasp of optionals and types in Swift before calling an API. Parsing the JSON file (and this was not a simple JSON file) entailed predicting what data type would be at every step of the process (arrays of objects within objects, generally) and forcing the JSON to conform to that data type. You had to constantly unwrap optionals by checking for nil values.

2. If you look for an array index that doesn’t exist, your app will crash.

JavaScript makes life easier by failing quietly: if you’re parsing JSON in JavaScript, you can casually check if anything exists at json.object.subobject[0] and if it doesn’t, your app will NOT explode. However, if you check for an array index (like 0) that doesn’t exist in Swift, your app WILL crash. And don’t even get me started on error handling in Swift — it’s not as simple as a try catch block!

Luckily, someone on Stack Overflow wrote a wonderful extension on the Array class that gracefully handles potential index out of range errors.

3. If you’re modifying the UI, you must do it in the main thread.

While I parsed the JSON response from the API call, I attempted to integrate my updates into the user interface and show the translation results. Rookie mistake! Swift makes the API call in a background thread, but all updates to the UI must occur in the main thread.

Fortunately, you can integrate UI changes into the background thread by surrounding all your UI modifications with this code:


DispatchQueue.main.async {
// Your UI changes here
}

I love the beauty of Swift’s strong typing and airtight compiling, but it can also be a huge nuisance. Calling an API in Swift isn’t for the faint of heart (start with JavaScript/jQuery), but using external APIs will undoubtedly enhance your apps.

You can check out the code for my Italian dictionary app on GitHub. I’m sure my app isn’t unique enough to go into the iTunes store (one of Apple’s requirements is uniqueness), but hopefully it can be a helpful example of calling an API in Swift 3!