I create a very simple json data for practice, but it always decode error when JSONDecoder().decode. I try some way to modify my struct, but all gets same error(prints "error0"). The code is at below.
struct ss : Codable {
var a : String
var b : String
}
let js = "[{\"a\":\"1\",\"b\":\"2\"},{\"c\":\"3\",\"d\":\"4\"}]"
let data = js.data(using: .utf8)
let a = [ss].self
do {
if let s = try? JSONDecoder().decode(a, from : data!) {
print(s[0].a)
}else{
print("error0")
}
}catch{
print("error1")
}
print("error1"): No, doprint("error1: \(error)"). There is anerrorobject that can have information, usefull information and can differ. So read it, no "error1" only.