我的应用程序在追求null json时崩溃 很久以来我一直想解决这个问题 您是最能协助我解决这个问题的人
func downloadJsonWithURLJB() {
let url=URL(string:"http://ccm-hotels.com/RixosJBR/IOS/api/Con4s.php")
do {
let allContactsData = try Data(contentsOf: url!)
let allContacts = try JSONSerialization.jsonObject(with: allContactsData, options: JSONSerialization.ReadingOptions.allowFragments) as! [String : AnyObject]
if let arrJSON = allContacts["Con+4"] as? [[String : Any]] {
for index in 0...arrJSON.count-1 {
let aObject = arrJSON[index] as! [String : AnyObject]
Con4Array.append(Con4s(DateCon4: (aObject["Date"] as? String)!, TimeCon4: (aObject["Time"] as? String)!,GuestNameCon4: (aObject["GuestName"] as? String)!, RoomCon4: (aObject["Room"] as? String)!, LimoCoCon4: (aObject["LimoCo"] as? String)!, DriverCon4: (aObject["Driver"] as? String)!, VehicleCon4: (aObject["Vehicle"] as? String)!, FlightCon4: (aObject["Flight"] as? String)!, PickUpCon4: (aObject["PickUp"] as? String)!, DropToCon4: (aObject["DropTo"] as? String)!, PaxCon4: (aObject["Pax"] as? String)!,TotalCon4: (aObject["Total"] as? String)!, CompleteCon4: (aObject["complete"] as? String)!))
}
}
DispatchQueue.main.async {
self.tableCon4.reloadData()
}
}
catch {}}
答案 0 :(得分:1)
例外
不能与upperBound
形成范围
由于丑陋的 objective-c-ish for
循环而出现。如果数组为空,则上限为-1,小于下限。
在Swift 从不中,如果实际上不需要索引,请使用基于索引的for
循环。
替换
for index in 0...arrJSON.count-1 {
let aObject = arrJSON[index] as! [String : AnyObject]
与
for aObject in arrJSON {
如果您确实需要索引,请使用enumerated()
语法
for (index, aObject) in arrJSON.enumerated() {
或安全半开范围运算符
for index in 0..<arrJSON.count {
其他不良做法:
Data(contentsOf
从远程URL加载数据。[String:Any]
,而不是[String:AnyObject]
。.allowFragments
毫无意义。