我遇到了这个错误的麻烦。
您能告诉我如何修复此代码以进行锻炼吗?
任何帮助将不胜感激。
//这是JSON数据。
{
"boxOfficeResult":{
"boxofficeType":"일별 박스오피스",
"showRange":"20161020~20161020",
"dailyBoxOfficeList":[
{
"rnum":"1",
"rank":"1",
"rankInten":"0",
"rankOldAndNew":"OLD",
"movieCd":"20153444",
"movieNm":"럭키",
"openDt":"2016-10-13",
"salesAmt":"1452501900",
"salesShare":"60.2",
"salesInten":"-85908900",
"salesChange":"-10",
"salesAcc":"23329189948",
"audiCnt":"193801",
"audiInten":"-11203",
"audiChange":"-10",
"audiAcc":"2879481",
"scrnCnt":"968",
"showCnt":"4961"
}, ...
//这是代码。
var list = Array<MovieVO>()
override func viewDidLoad() {
let apiURI = NSURL(string: "http://www.kobis.or.kr/kobisopenapi/webservice/rest/boxoffice/searchDailyBoxOfficeList.json?key=a7497a5e700fdff2f3fd468b604d9d18&targetDt=20161020")
let apidata : NSData? = NSData(contentsOf: apiURI! as URL)
NSLog("result = %@", NSString(data: apidata! as Data, encoding: String.Encoding.utf8.rawValue)!)
do {
let apiDictionary = try JSONSerialization.jsonObject(with: apidata! as Data, options: []) as! NSDictionary
let boxOfficeResult = apiDictionary["boxOfficeResult"] as! NSDictionary
let dailyBoxOfficeList = boxOfficeResult["dailyBoxOfficeList"] as! NSArray
var mvo : MovieVO
for row in dailyBoxOfficeList {
mvo = MovieVO()
mvo.rank = row["rank"] as? String // error occur
mvo.movieNm = row["movieNm"] as? String
mvo.openDt = row["openDt"] as? String
mvo.audiCnt = row["audiCnt"] as? String
self.list.append(mvo)
}
} catch {
}
}
答案 0 :(得分:1)
最短的快速修复将改变这一行:
let dailyBoxOfficeList = boxOfficeResult["dailyBoxOfficeList"] as! NSArray
这样的事情:
let dailyBoxOfficeList = boxOfficeResult["dailyBoxOfficeList"] as! [[String: Any]]
在Swift 3中,NSArray
的元素类型已变为Any
,您无法将任何方法(包括下标)应用于Any
。在您的代码中,row
的类型被推断为Any
,您无法将下标应用于row
。
更进一步让你的代码更加流畅和安全:
enum MyError: Error {
case badURL
case notDictionary
case invalidBoxOfficeResult
case invalidDailyBoxOfficeList
}
override func viewDidLoad() {
do {
//Use `URL` rather than `NSURL`
guard let apiURI = URL(string: "http://www.kobis.or.kr/kobisopenapi/webservice/rest/boxoffice/searchDailyBoxOfficeList.json?key=a7497a5e700fdff2f3fd468b604d9d18&targetDt=20161020") else {
throw MyError.badURL
}
//Use `Data` rather than `NSData`
//Avoid using forced unwrapping
let apidata = try Data(contentsOf: apiURI)
//Use `String` rather than `NSString`
NSLog("result = %@", String(data: apidata, encoding: .utf8)!)
//Cast the resutl of type `Any` as soon as possible
guard let apiDictionary = try JSONSerialization.jsonObject(with: apidata) as? [String: Any] else {
throw MyError.notDictionary
}
//Cast the resutl of type `Any` as soon as possible
guard let boxOfficeResult = apiDictionary["boxOfficeResult"] as? [String: Any] else {
throw MyError.invalidBoxOfficeResult
}
//Cast the resutl of type `Any` as soon as possible
guard let dailyBoxOfficeList = boxOfficeResult["dailyBoxOfficeList"] as? [[String: String]] else {
throw MyError.invalidDailyBoxOfficeList
}
for row in dailyBoxOfficeList {
var mvo = MovieVO() //<- Assuming `MovieVO` is a struct, use `let` if it is a class
mvo.rank = row["rank"]
mvo.movieNm = row["movieNm"]
mvo.openDt = row["openDt"]
mvo.audiCnt = row["audiCnt"]
self.list.append(mvo)
}
} catch {
print(error) //<- You should not ignore errors
}
}
还有一点......
Data(contentsOf:)
(或NSData(contentsOf:)
)可能会无限期地消耗并阻塞主线程,直到收到整个数据。如果您正在为App Store编写应用程序,则此类应用程序可能会被拒绝。尝试在后台线程中加载内容。