我正在尝试将数据从Firestore
解码为json格式,但是出现了该错误。
这是模型课
class UserModel : Codable{
var email: String?
var name: String?
var roles: [Roles]?
init(email: String, name: String, roles: [Roles]) {
self.email = email
self.name = name
self.roles = roles
}
}
这是“角色”结构
struct Roles: Codable {
var role: String?
enum CodingKeys: String, CodingKey{
case role = "student"
}
}
我检查了stackoverflow的一些答案,但没有任何帮助。
这是我解码的方式:
extension DocumentSnapshot {
func decode<T: Decodable>(as objectType: T.Type, includingId: Bool = true) throws -> T {
var documentJson = data()
if includingId {
documentJson?["id"] = documentID
}
let documentData = try JSONSerialization.data(withJSONObject: documentJson ?? "null", options: [])
let decodedObject = try JSONDecoder().decode(objectType, from: documentData)
return decodedObject
}
}
这是我的Json文件
{
name: "pisal",
email: "email@gmail.com",
roles: [
myrole : "student"
]
}