Reputation: 225
I am using http://www.omdbapi.com/?t=pulp+fiction IMDB api for "pulp fiction" in my Code.
There is a search bar in the application and I write "pulp fiction" to this search bar then enter. I get this error.
Could not cast value of type '__NSDictionaryM' (0x1055952b0) to 'NSString' (0x1023e3c60).
ViewController.swift:
//
// ViewController.swift
// IMDB Api Project
//
// Created by gurkan on 5.05.2017.
// Copyright © 2017 gurkan. All rights reserved.
//
import UIKit
class ViewController: UIViewController,UISearchBarDelegate {
@IBOutlet weak var searchBar: UISearchBar!
@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var directorLabel: UILabel!
@IBOutlet weak var ratingLabel: UILabel!
@IBOutlet weak var actorsLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
searchBar.delegate = self
}
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
searchForMovie(title: searchBar.text!)
searchBar.text = ""
}
func searchForMovie(title: String){
//http://www.omdbapi.com/?t=pulp+fiction
if let movie = title.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed){
let url = URL(string: "http://www.omdbapi.com/?t=\(movie)")
let session = URLSession.shared
let task = session.dataTask(with: url!, completionHandler: { (data, response, error) in
if error != nil {
print(error!)
} else {
if data != nil {
do {
let jsonResult = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers)
as! String //Error Line!
let jsonString = jsonResult.components(separatedBy: "")
let jsonDict = jsonString as! Dictionary<String,String>
DispatchQueue.main.async {
print(jsonDict)
}
} catch {
}
}
}
})
task.resume()
}
}
}
How can I solve this problem?
Upvotes: 1
Views: 3750
Reputation: 12842
There's two things wrong.
First, this line force unwraps to a String
let jsonResult = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers)
as! String
Simple remove as! String
- it's not needed. Swift will infer the type and correctly create jsonResult
.
Second, your code assumes that the JSON response coming back is a dictionary made up entirely of String
names, and String
values. Looking at the response from the url you posted, the object associated with the Ratings
value is actually a dictionary. Since you are force unwrapping jsonString
into a dictionary of strings, that will fail, since it is NOT a dictionary of strings. It's a dictionary of Strings and other stuff - a dictionary.
The easiest fix is:
Replace this line:
let jsonDict = jsonString as! Dictionary<String,String>
with
let jsonDict = jsonString as! [String: AnyObject]
Upvotes: 3