Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Dawid Cedrych
15,108 PointsHow to get json array in swift?
I finished the weather app called Stormy in swift, that was a really useful course for me. However, I attempted to apply gained skills in practice and encountered a big problem. My JSON data is JSON Array, not object, I've changed the code in such way that each NSDictionary in JSON serialization is replaced with NSArray. But unfortunately it's not working at all( Still following error occurs: EXC_BREAKPOINT (code=EXC_I386_BPT, subcode=0x0) error )
I showed my problem in stackoverflow forum, unfortunately without any response ( http://stackoverflow.com/questions/27234541/json-array-deserialization-in-swift-still-getting-the-exc-breakpoint-code-exc ). I decided to post it here as my code is exactly the same as in your weather app.
Pasan Premaratne or Amit Bijlani , would you guys be so kind to take a look at my issue?
Thanks in advace
4 Answers
Stephen Whitfield
16,771 PointsLooks like your syntax is all wrong. By the way, just a suggestion, you don't need to specify a return type Void for your function. Just form it the way I have in my correction code.
func getCurrentLunchData()
{
let lunchURL = NSURL(string: "http://92.222.218.68:3000/lunches/getlunches.json?c_lat=50.2&c_lon=19.9")
let sharedSession = NSURLSession.sharedSession()
let downloadTask: NSURLSessionDownloadTask = sharedSession.downloadTaskWithURL(lunchURL!, completionHandler: { (url: NSURL!, response: NSURLResponse!, error: NSError!) -> Void in
if (error == nil) {
let dataObject = NSData(contentsOfURL: lunchURL!)
let restaurantArray: NSArray = NSJSONSerialization.JSONObjectWithData(dataObject!, options: nil, error: nil) as NSArray
//let nearbyRestaurants = Restaurant(restaurantArray: restaurantArray)
dispatch_async(dispatch_get_main_queue(), { () -> Void in
println("Insert UI here")
})
}
})
downloadTask.resume()
}
Dawid Cedrych
15,108 PointsThank you. My syntax was ok, the error I had was a runtime error. Another thing is, the code you pasted has error with "!" operand after lunchURL costant. Are you sure the code above is correct?
Nevertheless thank you for your attention!
Stephen Whitfield
16,771 PointsNope, it's not an error. The parameter in that class method for NSData is an optional and needs to be passed an unwrapped NSURL object. I'm very sure the code above is correct.
Dawid Cedrych
15,108 PointsI believe it works, but I'm still having the problem with syntax. Here is a link to the project with corrected code ( it's really simple, it's just the view controller and one additional class with variables, almost identical to weather app).
https://github.com/thedc89/LTB
Could you take a look and checked if you are able to get this json, parse it and get it working?
Thank you!
Dawid Cedrych
15,108 PointsSlight modification:
let lunchURL:NSURL?
let dataObject:NSData?
Now works perfectly ;)