1

I'm sorry if this may seem that I haven't read or updated my information on Swift 2.2 but I actually already have and I've gone through the updated method but still am receiving errors(I didn't state them here because thats not my issue; my issue is writing the JSONObjectWithData in its updated syntax form)

I took this function from an older swift book and am trying to get it to Parse the data into JSON. I used the updated syntax for the method JSONObjectWithData() but wasn't able to piece the syntax pieces together. My problem isn't the compiler screaming at me with errors because I know that I was rewriting the JSONObjectWithData method wrong, heres the old syntax that I tried to rewrite but couldn't do so successfully.

I still haven't wrapped my head around the concept of parsing data into JSON even after studying the chapter and reading the Apple documentation, let alone try to rewrite the JSONObjectWithData method correctly. I searched a similar answer to this and could not figure out how to write this book method correctly in the updated syntax and have it run without errors. I've been stuck on this for 2 days.

func parseJson(data: NSData) {           
    var error: NSError?
    let json: AnyObject? = NSJSONSerialization.JSONObjectWithData(data, options: .allZeros, error: &error)           
    if error == nil {
        if let unwrappedJson: AnyObject = json {
            parseSongs(json: unwrappedJson)
        }
    }
}
Alex
  • 37
  • 7
  • 1
    You fail to explain what your actual issue is. What error do you get? What doesn't work for you? Didn't try, but the code looks kinda OK. – hnh Aug 28 '16 at 20:02
  • try `if let unwrappedJson = json as? AnyObject { /* }` – Özgür Ersil Aug 28 '16 at 20:32
  • @hnh I'm just trying to update this code to Swift 2.2, theres a newer syntax to the JSONObjectWithData method and I've already read it and tried rewriting this older version; I'm just confused on how to transfer this older syntax into the newer syntax correctly. I would have been able to if I knew how to parse data into JSON but I'm not familiar with that and this is part of a step-by-step program that was written in an older swift book. – Alex Aug 28 '16 at 22:42
  • Again: What errors do you get where. Be more specific. I may be missing sth but it does look like Ok Swift 2.2 code to me. Maybe the as? as mentioned by Özgür? – hnh Aug 28 '16 at 22:46
  • The error it give me is " Extra argument label in call" Its for the .JSONObjectWithData call.... @hnh and I tried the line in the comment above, it didn't work. – Alex Aug 28 '16 at 22:59
  • OK, I see. Drop the error parameter and use 'try' – hnh Aug 28 '16 at 23:03
  • @hnh ok, its gives me the error "expected expression in list of expressions"... – Alex Aug 28 '16 at 23:06
  • 1
    Possible duplicate of http://stackoverflow.com/questions/31073497/swift-extra-argument-error-in-call – Eric Aya Aug 29 '16 at 08:08
  • @EricAya yes that was the exact question I looked into before asking mine, I can't seem to piece the method together into the updated syntax. I'm very confused on what to put in the new throwing) method;( try/catch syntax). I just want this write this correctly in the updated "throwing" method, syntax-wise. – Alex Aug 29 '16 at 16:38
  • @EricAya ok this make more sense, but I'm still confused on how to word it all....the compiler is screaming at me with the `.allZeros` parameter. It may have been replaced with something else. And also if NSError is removed then how can I declare a variable to put the error in; where would I declare it and what type would it be of? – Alex Aug 29 '16 at 16:53
  • ok fair enough, I'll do that. Sorry for the headache man! @EricAya – Alex Aug 29 '16 at 17:03
  • 5 hours later....headache still there.....I can't seem to wrap my head around rewriting this. @EricAya – Alex Aug 29 '16 at 22:46

1 Answers1

0

Have you tried this block of code:

let json: AnyObject? = try! NSJSONSerialization.JSONObjectWithData(data, options: .AllowFragments)
Rashwan L
  • 38,237
  • 7
  • 103
  • 107
  • Thank you I was able to get that to work, and this line of code also works with what I had written. I guess I'll use your line of code, which seems better. I do have a screenshot of one of my functions thats really confusing me, can I quickly send it to you and maybe you can just glance at it and give me some insight. It's very short. – Alex Sep 07 '16 at 04:18
  • https://twitter.com/RidhamOfficial/status/773381104430673920 I can't enter the chat, so I uploaded it on my twitter, here is the link. – Alex Sep 07 '16 at 04:44