当我尝试从具有日期变量的数据库中解码 JSON 对象时,出现错误“由于未捕获的异常‘NSInvalidArgumentException’而终止应用程序,原因:‘JSON 写入中的无效类型 (__NSTaggedDate)’”。错误发生在以下代码行: let jsonData = try JSONSerialization.data(withJSONObject: self) 中:
extension Array where Element == Dictionary<String,Any> {
func decode<T>() throws -> [T] where T: Codable {
let jsonData = try JSONSerialization.data(withJSONObject: self)
// 1.
let decoder = JSONDecoder()
// 2.
return try decoder.decode([T].self, from: jsonData)
}
}
Run Code Online (Sandbox Code Playgroud)
我使用此方法使用 PG 依赖项将 Postgres SELECT 查询的结果转换为 Codable 对象数组。
case .success(let result):
let array = result.rows.map { Dictionary($0) }
let objects = try! array.decode() as [Model]
completion(objects,nil)
Run Code Online (Sandbox Code Playgroud) 无法更改导航栏背景颜色。我尝试将颜色设置为 navigationController?.navigationBar.backgroundColor 和 navigationController?.navigationBar.barTintColor,例如以下示例中的 UIColor.red,但是白色 UIImage 出现在视图层次结构中导航栏颜色上方,如下所示图片: https: //imguh.com/image/CBXaj
还尝试使导航栏半透明和不透明,但没有效果。我可以更改按钮颜色并自定义其他元素,但不能更改栏的背景颜色。感谢任何帮助。