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.
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.
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:
// Your UI changes here
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!