你好我在與NSJSONSerialization從空氣污染指數JSON問題JSONObjectWithData錯誤:意外發現零而展開的可選值
代碼:
func json() {
let urlStr = "https://apis.daum.net/contents/movie?=\etc\(keyword)&output=json"
let urlStr2: String! = urlStr.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLHostAllowedCharacterSet())
let url = NSURL(string: urlStr2)
let data = NSData(contentsOfURL: url!)
do {
let ret = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions(rawValue: 0)) as! NSDictionary
let channel = ret["channel"] as? NSDictionary
let item = channel!["item"] as! NSArray
for element in item {
let newMovie = Movie_var()
// etc
movieList.append(newMovie)
}
catch {
}
}
而且我收到此錯誤
let ret = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions(rawValue: 0)) as! NSDictionary
致命錯誤:意外發現零,同時展開一個可選值
如何修復它?
檢查數據是否爲nil,下一次請正確格式化您的代碼。 –