Search code examples
iosmemory-managementswiftlinkedin-api

parsing json in swift


I'm trying to read the Linkedin response in swift.

My object is something like this ["positions":["values":["data1","data2","data3"]]]

        if let positions: NSDictionary = info["positions"] as NSDictionary!{
        if let positionsInfo: [NSDictionary] = positions["values"] as? [NSDictionary]{
            for position : NSDictionary! in positionsInfo {
                dosomething(position, person:usr)
            }
        }
    }

If I do a StepOver line by line it works correctly. But if I run it i'll get a EXC_BAD_ADDRESS(code=1,address=0x7966b04) I enabled Zombie objects and ran it on Instruments. I'm pretty sure this is the code which is causing the problem. But not sure what is wrong with it.


Solution

  • The moment you used ! you opened yourself up for crashes if there were any problem. You must use as? to make sure that the data is actually what you think it is.

    There are many blog posts out there on how to safely parse JSON into Swift data structures. It's now almost a rite of passage for Swift bloggers.