tncb tncb - 21 days ago 7
iOS Question

Can't convert value of type - Swift

I'm having an issue with parsing in Swift where it says I can't convert value of type [Dictionary] to type [String:AnyObject]
This is the line that causes the error

self.grabData(json: json as [String : AnyObject])


This is the whole code. Thanks!

func jsonParser() {
let urlPath = "http://csmadison.dhcp.bsu.edu/~vjtanksale/cs320/selectstudents.php"
guard let endpoint = URL(string: urlPath) else {
print("Error creating endpoint")
return
}
let request = URLRequest(url: endpoint)
URLSession.shared.dataTask(with: request) { (data, response, error) in
do {
guard let data = data else {
return
}
guard let json = try JSONSerialization.jsonObject(with: data, options: []) as? [Dictionary<String, Any?>] else {
print("No idea")
return
}
print(json)
self.grabData(json: json as [String : AnyObject]) // This is where I get the error
} catch let error as NSError {
print(error.debugDescription)
}
}.resume()
}

func grabData(json: [String:AnyObject]) {
guard let searchResult = SearchResult(json: json) else {
return
}

self.results.append(searchResult)

DispatchQueue.main.async {
self.listTableView.reloadData()
}
}

Answer

The reason you are getting an error is because you are trying to convert a [[String:Any?]] to [String:AnyObject].

Try changing

guard let json = try JSONSerialization.jsonObject(with: data, options: []) as? [Dictionary<String, Any?>]

to

guard let json = try JSONSerialization.jsonObject(with: data, options: []) as? [[String:AnyObject]]

Then you need to loop through each object of your JSON result:

for result in json {
    if let student = SearchResult(json: result) {
        self.results.append(student)
    }
}

self.grabData()

Also, change your grabData function to the code below, or combine the reloading of the table view with your jsonParser function.

func grabData() {
    DispatchQueue.main.async {
        self.listTableView.reloadData()
    }
}

So all together it would like:

func jsonParser() {
    let urlPath = "http://csmadison.dhcp.bsu.edu/~vjtanksale/cs320/selectstudents.php"
    guard let endpoint = URL(string: urlPath) else {
        print("Error creating endpoint")
        return
    }
    let request = URLRequest(url: endpoint)
    URLSession.shared.dataTask(with: request) { (data, response, error) in
        do {
            guard let data = data else {
                return
            }
            guard let json = try JSONSerialization.jsonObject(with: data, options: []) as? [[String:AnyObject]] else {
                print("No idea")
                return
            }
            print(json)

            for result in json {
                if let student = SearchResult(json: result) {
                    self.results.append(student)
                }
            }
            self.grabData()
        } catch let error as NSError {
            print(error.debugDescription)
        }
        }.resume()
}

func grabData() {    
    DispatchQueue.main.async {
        self.listTableView.reloadData()
    }
}