我在Swift中使用 table.row.add( {
//you dynamic data
"name": "Tiger Nixon",
"position": "System Architect",
"salary": "$3,120",
"start_date": "2011/04/25",
"office": "Edinburgh",
"extn": "5421"
} ).draw();
,并且需要获得更好的错误消息。
在调试描述中(例如),我可以看到诸如“给定数据不是有效的JSON”之类的消息,但我需要知道的是,而不是网络错误(例如)。
JSONDecoder()
我试图强制转换为 let decoder = JSONDecoder()
if let data = data{
do {
// process data
} catch let error {
// can access error.localizedDescription but seemingly nothing else
}
,但这似乎并没有显示更多信息。我当然不需要字符串-甚至错误代码也比这有用得多。
答案 0 :(得分:4)
从不在解码error.localizedDescription
块中打印catch
。这将返回一个毫无意义的通用错误消息。始终打印error
实例。然后,您会得到所需的信息。
let decoder = JSONDecoder()
if let data = data {
do {
// process data
} catch {
print(error)
}
或者对于完整组错误使用
let decoder = JSONDecoder()
if let data = data {
do {
// process data
} catch let DecodingError.dataCorrupted(context) {
print(context)
} catch let DecodingError.keyNotFound(key, context) {
print("Key '\(key)' not found:", context.debugDescription)
print("codingPath:", context.codingPath)
} catch let DecodingError.valueNotFound(value, context) {
print("Value '\(value)' not found:", context.debugDescription)
print("codingPath:", context.codingPath)
} catch let DecodingError.typeMismatch(type, context) {
print("Type '\(type)' mismatch:", context.debugDescription)
print("codingPath:", context.codingPath)
} catch {
print("error: ", error)
}