Search code examples
iosobjective-cjsonios7swift

Error on serializer JSON using Swift - AutoreleasingUnsafePointer (has 1 child)


I was try parse a JSON online, but cannot read this how the types generally, how:

   typealias JSON = AnyObject
    typealias JSONDictionary = Dictionary<String, JSON>
    typealias JSONArray = Array<JSON>

Never return this types, the JSON i read is:

json:

[
{
"id":72,
"name":"Batata Cremosa",
"time":"1:30 horas",
"rcp_img_file_name":"batata-cremosa.jpg"
},
{
"id":183,
"name":"Caldeirada de Peixes",
"time":"50 minutos",
"rcp_img_file_name":"caldeirada-peixes.jpg"
},
{
"id":76,
"name":"Batata com Cebola e Ervas",
"time":"10 minutos",
"rcp_img_file_name":"batata-cebola-ervas.jpg"
},
{
"id":56,
"name":"Arroz de forma",
"time":"25 minutos",
"rcp_img_file_name":"arroz-forma.jpg"
}]

The json is read and cast in a string how this in console:

in console(var jsonString):

  (
            {
            id = 72;
            name = "Batata Cremosa";
            "rcp_img_file_name" = "batata-cremosa.jpg";
            time = "1:30 horas";
        },
            {
            id = 183;
            name = "Caldeirada de Peixes";
            "rcp_img_file_name" = "caldeirada-peixes.jpg";
            time = "50 minutos";
        },
            {
            id = 76;
            name = "Batata com Cebola e Ervas";
            "rcp_img_file_name" = "batata-cebola-ervas.jpg";
            time = "10 minutos";
        },
            {
            id = 463;
            name = "Pat\U00ea de Frango F\U00e1cil";
            "rcp_img_file_name" = "pate-frango-facil.jpg";
            time = "30 minutos";
        },
...

So, this is the func that dont read the JSON, by debug when have to choice around the Objects dont enter in anyone option:

parse function:

func JSONParseArray(jsonString: String) {
        println(jsonString)

        var data: NSData = jsonString.dataUsingEncoding(NSUTF8StringEncoding)

        var error: AutoreleasingUnsafePointer<NSError?> = nil


        let jsonObj: AnyObject? = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions(0),
                error: error)
        println("Error: \(error)")

        if jsonObj is JSONDictionary {
            println("0")

        }
        else if  jsonObj is JSONArray {
            println("1")

        }

    }

Dont return Array or Dictionary, in debug appear this below the json:

error returned:

Error: VSs26AutoreleasingUnsafePointer (has 1 child)

and when i debug the jsonObj this return nil

Please someone help, i search by hours by info, test many things but no one works for me, thanks much.


Solution

  • It is easier than you think:

    var error: NSError?
    let jsonObj: AnyObject? = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions(0),
                error: &error)
    

    Additionally there is the question of why the JSON is being passed in as a string and not data.

    jsonObj is a bad name because once it is de-serialized it is going to be an Array, not JSON.