subscript Ich bin neu in Swift und versuche, eine Anwendung zum Parsen von Json von einer Webseite zu schreiben. Allerdings bekomme ich den Fehler "Kann nicht einen Wert vom Typ '[Wörterbuch]'"Kann keinen Wert des Typs - Swift 3
Wie kann ich das beheben? Es tritt mehrmals auf. Folgendes ist mein Code. Vielen Dank!
func grabData(results: [Dictionary<String, Any?>]){
guard let resultList = results as? [Dictionary<String, Any?>] else { return }
for iResult in resultList {
var tempResult = SearchResult()
tempResult.studentId = self.getInt(dict: [iResult], string: "StudentId")
tempResult.LastName = self.getString(dict: [iResult], string: "LastName")
tempResult.FirstName = self.getString(dict: [iResult], string: "FirstName")
tempResult.Major = self.getString(dict: [iResult], string: "Major")
tempResult.Year = self.getString(dict: [iResult], string: "Year")
tempResult.GPA = self.getDouble(dict: [iResult], string: "GPA")
self.results.append(tempResult)
}
DispatchQueue.main.async {
self.listTableView.reloadData()
}
}
func getString(dict: [Dictionary<String, Any?>], string: String) -> String? {
if let result = dict[Dictionary<String, Any?>] as? String {
return result
}
return nil
}
func getInt(dict: [Dictionary<String, Any?>], string: String) -> Int? {
if let result = dict[string] as? Int {
return result
}
return nil
}
func getDouble(dict: [Dictionary<String, Any?>], string: String) -> Double? {
if let result = dict[string] as? Double {
return result
}
return nil
}
Vielleicht möchten Sie eine Bibliothek für die Analyse von JSON verwenden, dies würde Ihr Leben ziemlich einfach machen ... Ich schrieb eine Bibliothek, die unter https://github.com/borchero/WebParsing verfügbar ist. – borchero
Vermeiden Sie die Angabe optionaler Werttypen in Swift-Wörterbüchern. Sie sind nicht sehr sinnvoll. – vadian