Ich habe ein Problem mit der Analyse von JSON.JSON NSBundle Error - Thread 1: EXC_BAD_INSTRUCTION (Code = EXC_I386_INVOP, Subcode = 0x0)
Dies ist die Struktur von JSON:
[{"id":33,"name":"5","sort":2},{"id":34,"name":"6","sort":3},{"id":35,"name":"7","sortOrder":4}]
Und nach dem Laufen Ich habe fatalerror:
Thread 1:EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)” with code "fatalError("error")"
Problem ist mit NSBundle. Ich nehme an, weil ich Code hinzugefügt habe:
guard let path = NSBundle.mainBundle().pathForResource("sections", ofType: "json"),
let jsonData = NSData(contentsOfFile: path) else {
fatalError("error")
}
do {
if let sections = try NSJSONSerialization.JSONObjectWithData(jsonData,
options: .MutableContainers) as? [String: AnyObject] {
// work with sections
let addr = Address(dict: sections)
print("sections")
}
} catch let error as NSError {
print(error)
}
Aber es gibt keine "Abschnitte". Tatsächlich hat die Verbindung mit JSON keinen Pfad - es gibt nur Objekte (ID, Name, Sortierung usw.). Also, wie sollte ich Guard ändern lassen Pfad = NSBundle, um dieses Problem zu verwalten?
Haben Sie in Ihrer App eine Datei 'sections.json'? Wenn nicht, was machst du mit "NSBundle"? – Larme
Nein, JSON wird von der URL als Zeichenfolge übernommen. Also was soll ich anstelle von NSBundle verwenden? – Szekspir
Wie bekommst du die Schnur? Holen Sie es, und wandeln Sie es in NSData um (es gibt eine Methode dafür). – Larme