A
A
Archakov Dennis2015-10-07 17:31:29
JSON
Archakov Dennis, 2015-10-07 17:31:29

Handling JSON data in Swift?

I decided to process the JSON data received from the server into an array, but it gives an error:

func parseJSON(data: NSData) -> NSDictionary{
        let jsonResult: NSDictionary = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers) as! NSDictionary
        return jsonResult;
    }

Call can throw, but it is not marked with 'try' and the error is not handled

Well, I put everything in try , after which it already displays an error:
Variable 'boardsDictionary' used before being initialized

func parseJSON(inputData: NSData) -> NSDictionary{
        var boardsDictionary: NSDictionary;
        do {
        try boardsDictionary = NSJSONSerialization.JSONObjectWithData(inputData, options: NSJSONReadingOptions.MutableContainers) as! NSDictionary
        } catch {}
        return boardsDictionary;
    }

How to correctly enter data into my function and output an array already.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
lyeskin, 2015-10-07
@lyeskin

There are ready-made mini-libraries for parsing JSON, why bother?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question