Yousef Abu Sallamah Yousef Abu Sallamah - 1 year ago 82
JSON Question

issue retrieve json data in swift iOS

I have issue that I can't retrieving json data in swift, I kept getting error message
this image describe the issue I have.

var url = NSURL(string: getDataURL)!
var data = NSData(contentsOf: url as URL)
do {
jsonArray = try JSONSerialization.jsonObject(withData: data, options: .allowFragments)!
catch let error {

I was working with Objective C but I want to switch to Swift language

Answer Source

Folks, please use the code completion feature to find the proper syntax

When you type JSONSerialization. you will see

enter image description here

The appropriate method is

JSONSerialization.jsonObject(with:data, options: .allowFragments)

Or press ⇧⌘0 (zero not O) and look up the method in the documentation by typing or pasting the class. Every developer must be able to read the documentation.

Aside from that you have to cast the result of the deserialization to the expected type

jsonArray = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as! [[String:Any]]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download