我正在從JSON中獲取一個多維數組並嘗試將其存儲到一個NSArray對象中。然後我想通過選取器迭代該對象。這工作在Swift 2上,但是一旦我轉換爲swift 3,在我的pickerView函數中得到'Type Any沒有下標成員'的錯誤。Swift 3 JSON多維NSArray字符串
相關的代碼(名字和麪孔改變):
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
let ticket_number = pvds_ticket_number[row]["ticket_number"] as! String
let customer_name = pvds_ticket_number[row]["customer_name"] as! String
return "\(ticket_number) - \(customer_name)"
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
{
let ticket_number = pvds_ticket_number[row]["ticket_number"] as! String
txt_ticket_number.text = ticket_number
}
func get_tickets()
{
let userID = UserDefaults.standard.string(forKey: "userID")
let url_path: NSString = "my_url_that_isnt_relevant" as NSString;
let url_path_formatted: NSString = url_path.addingPercentEscapes(using: String.Encoding.utf8.rawValue)! as NSString
let url = URL(string: url_path_formatted as String)
let session = URLSession.shared
let task = session.dataTask(with: url!, completionHandler: {data, response, error -> Void in
if(error != nil) {
// If there is an error in the web request, print it to the console
print(error!.localizedDescription)
}
do {
let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
if let parseJSON = json {
if let results: NSArray = parseJSON["results"] as? NSArray{
DispatchQueue.main.async(execute: {
self.pvds_ticket_number = results as! [Any] as NSArray
self.pv_ticket_number.reloadAllComponents()
})
}
}
} catch {
print("something went wrong")
}
})
task.resume()
}
這些行顯示的錯誤:
讓TICKET_NUMBER = pvds_ticket_number [行] [ 「TICKET_NUMBER」]作爲!字符串
讓customer_name = pvds_ticket_number [row] [「customer_name」] as!字符串
你有沒有檢查這個http://stackoverflow.com/search?q=%5Bswift%5DType+Any+has+no+subscript+members –
沒有[這53個結果](http://stackoverflow.com/搜索?tab = votes&q =%5bswift3%5d%20Any%20has%20no%20subscript%20members)搜索「Any any no subscript members」有幫助嗎? – Moritz