【发布时间】:2018-01-16 13:15:49
【问题描述】:
我已将我的 iOS 项目从 swift 2.x 转换为 swift 3.x 我的代码中现在有 50 多个错误。最常见的一种是“无法强制解开非可选类型'AnyObject'的值”
下面是部分代码:
let documentURL = JSON[eachOne]["media"]!![eachMedia]["media_url"]! 行产生错误
我该如何解决这个问题?谢谢!
if let JSON = response.result.value as? [[String: AnyObject]]{
//print("JSON: \(JSON)")
myDefaultValues.userDefaults.setValue(JSON, forKey: "JSON")
for eachOne in 0 ..< (JSON as AnyObject).count{
// print("Cover: \(JSON[eachOne]["cover"])")
//Download all Covers
let documentURL = JSON[eachOne]["cover"]!
let pathus = URL(string: documentURL as! String)
if pathus != nil {
HttpDownloader.loadFileSync(pathus!, completion:{(path:String, error:NSError!) in
})
}
//Download all Media Files
if JSON[eachOne]["media"] != nil{
//print(JSON[eachOne]["media"]!)
//print(JSON[eachOne]["media"]!!.count)
let thisMediaView = JSON[eachOne]["media"]!.count
for eachMedia in 0 ..< thisMediaView!{
//print(JSON[eachOne]["media"]!![eachMedia]["media_url"])
**let documentURL = JSON[eachOne]["media"]!![eachMedia]["media_url"]!**
let pathus = URL(string: documentURL as! String)
if pathus != nil {
HttpDownloader.loadFileSync(pathus!, completion:{(path:String, error:NSError!) in
})
}
}
}
}
}
【问题讨论】:
-
你不应该再使用 AnyObject,使用“Any?”
-
在(let documentURL = JSON[eachOne]["media"]....)中分别定义:数组、字符串、字典
-
我尝试过使用“Any”,但它会产生相同的错误
-
@MAhipalSingh 你能给我举个例子吗?
-
让 JSON = JSON 为! Dictionary
让 dic1 = JSON["eachOne"] as! Dictionary 让 arrMedia = object["media"] as!数组类似这样的东西