gpt4 book ai didi

swift - 类型 'NSFastEnumerationIterator.Element' 没有下标成员

转载 作者:行者123 更新时间:2023-11-28 16:06:32 24 4
gpt4 key购买 nike

我遇到了这个错误。

你能告诉我如何修复此代码以进行锻炼吗?

我们将不胜感激。

//这是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 {

}
}

最佳答案

最短的快速修复是改变这一行:

        let dailyBoxOfficeList = boxOfficeResult["dailyBoxOfficeList"] as! NSArray

像这样:

        let dailyBoxOfficeList = boxOfficeResult["dailyBoxOfficeList"] as! [[String: Any]]

在Swift 3中,NSArray的Element类型变成了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 编写应用程序,则此类应用程序有被拒绝的风险。尝试在后台线程中加载内容。

关于swift - 类型 'NSFastEnumerationIterator.Element' 没有下标成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40200061/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com