无法将“__NSDictionaryM”类型的值 (0x1055952b0) 转换为 'NSString' (0x1023e3c60)
Could not cast value of type '__NSDictionaryM' (0x1055952b0) to 'NSString' (0x1023e3c60)
我在我的代码中为 "pulp fiction" 使用 http://www.omdbapi.com/?t=pulp+fiction IMDB api。
应用程序中有一个搜索栏,我在这个搜索栏中输入 "pulp fiction" 然后输入。
我收到这个错误。
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()
}
}
}
我该如何解决这个问题?
有两点不对。
首先,这条线强制展开为 String
let jsonResult = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers)
as! String
简单删除 as! String
- 不需要。 Swift 将推断类型并正确创建 jsonResult
.
其次,您的代码假定返回的 JSON 响应是一个完全由 String
名称和 String
值组成的字典。查看您发布的 url 的响应,与 Ratings
值关联的对象实际上是一个字典。由于您强制将 jsonString
解包到字符串字典中,这将失败,因为它不是字符串字典。它是字符串和其他内容的字典 - 字典。
最简单的修复方法是:
替换此行:
let jsonDict = jsonString as! Dictionary<String,String>
和
let jsonDict = jsonString as! [String: AnyObject]
我在我的代码中为 "pulp fiction" 使用 http://www.omdbapi.com/?t=pulp+fiction IMDB api。
应用程序中有一个搜索栏,我在这个搜索栏中输入 "pulp fiction" 然后输入。 我收到这个错误。
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()
}
}
}
我该如何解决这个问题?
有两点不对。
首先,这条线强制展开为 String
let jsonResult = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers)
as! String
简单删除 as! String
- 不需要。 Swift 将推断类型并正确创建 jsonResult
.
其次,您的代码假定返回的 JSON 响应是一个完全由 String
名称和 String
值组成的字典。查看您发布的 url 的响应,与 Ratings
值关联的对象实际上是一个字典。由于您强制将 jsonString
解包到字符串字典中,这将失败,因为它不是字符串字典。它是字符串和其他内容的字典 - 字典。
最简单的修复方法是:
替换此行:
let jsonDict = jsonString as! Dictionary<String,String>
和
let jsonDict = jsonString as! [String: AnyObject]