Ich habe ein seltsames Verhalten erlebt, wenn JSON-Daten-Parsing mit Swift 3.Seltsames Verhalten von optionals in Swift 3
do {
let json = try JSONSerialization.jsonObject(with: data!, options: []) as! NSDictionary
let items:[AnyObject] = (json["items"] as? [AnyObject])!
for item in items {
let id:String = item["id"] as! String
print("ID: \(id)")
let info = item["volumeInfo"] as AnyObject
print(info)
let title = info["title"]
print(title)
}
} catch {
print("error thrown")
}
Dies erzeugt die folgende Ausgabe. Beachten Sie, dass info optional ist, aber wenn ich versuche, es auszupacken, heißt es, dass es nicht optional ist! Das Skript stürzt unter let title = info["title"]
ab. Daher kann ich nicht auf den Titelschlüssel zugreifen. Dieses Verhalten hat sich seit Swift 2.
ID: lbvUD6LUyV8C
Optional({
publishedDate = 2002;
publisher = "Sams Publishing";
title = "Java Deployment with JNLP and WebStart";
})
Versuchte Ihre Lösung, aber jetzt, wenn ich drucken 'info 'Ich bekomme' Optional (Optional ({ publishedDate = "2011-12-28"; Herausgeber = "Jones & Bartlett Publishers"; title = "Programmierung mit Java: Ein Multimedia-Ansatz"; })) ' –
Wenn Sie ein [minimales, vollständiges und überprüfbares Beispiel] (http://stackoverflow.com/help/mcve) veröffentlichen, werde ich mehr als glücklich sein, das Problem mit Ihnen zu lösen. – Alexander
https://gitlab.com/covcom/stack –