-2

I am trying to parse an json from a webserver i am using and i can't actually manage to do that although i could back in iOS 8. This is the code i am using:

let str = "URL-THAT-HAS-THE-JSON"

let url = NSURL(string: str)

let data = NSData(contentsOfURL: url!)

var names : [String] = []
do
{
    let json = try NSJSONSerialization.JSONObjectWithData(data!, options: [] )
    if let blogs = json["blogs"] as? [[String: AnyObject]]
    {
        for blog in blogs
        {
            if let name = blog["name"] as? String
            {
                names.append(name)
            }
        }
    }
}
catch {
    print("error serializing JSON: \(error)")
}
print (names)

unfortunately i can't give you the url with the json but i can make sure it works on server side.Could someone give me some help on what i am doing wrong?

Korpel
  • 2,432
  • 20
  • 30
  • 1
    `options: nil` should be `options: []`, as (e.g.) in http://stackoverflow.com/questions/30769387/swift-2-0-calendar-components-error and some more similar questions. – Martin R Oct 18 '15 at 18:16
  • ok i just did that and switched from nil to [ ] but that doesn't seem to fix the issues cause i am still getting the EXC_BAD_INSTRUCTION error in that line. Any ideas what's wrong? – Korpel Oct 18 '15 at 18:18
  • You are force unwrapping `data`. Is it `nil`? – vacawama Oct 18 '15 at 18:21
  • 2
    What do you mean by "still getting"? Your original code did not compile at all, therefore it cannot crash at runtime. Also you did not describe the issue in the question, apart from *"i can't actually manage"*. – Martin R Oct 18 '15 at 18:21
  • yes data is nil so i am explicity unwrapping a nil value therefore my app crashes. Thanks – Korpel Oct 18 '15 at 18:23

1 Answers1

0

The answer to the question was that i was unwrapping data witha value of nil crashing my application. Thanks everyone for answering.

Korpel
  • 2,432
  • 20
  • 30