Working with JSON in swift

It is the first time I practice Swift programming.

The first app I am going to build is a catalogue of schools in Singapore. In order to do that, I create a file data.json with the following format:

To read this data, just call file and read it as a string:

Here come the tricky part, when I need to parse JSON from string, there are a lot of weird errors mainly coming from Swift. I try to read some online articles. Actually we can use NSData to convert it to NSDictionary or NSArray.

I strongly recommend using this library SwiftyJSON which saved me a lot of time to navigate data in JSON. Here is the code with SwiftyJSON :

It was really awesome. After few days, I figure out how to fix the unwrapped optional type value in Swift. As times goes by, you will see a lot of “if let var_a var_b” in Swift like the above snippet.

Have fun with Swift.

My repo is here: https://github.com/aladine/swift-catalogue

Advertisements