gpt4 book ai didi

json - JSON 序列化后子类属性崩溃

转载 作者:行者123 更新时间:2023-11-28 11:55:39 25 4
gpt4 key购买 nike

我有一个最奇怪的问题。在执行 JSON 序列化并创建对象后,代码在尝试访问子类的任何属性时崩溃。当我尝试访问对象父类(super class)的属性时,它们会被正确打印。

这是类:

import UIKit

class Patient: Person {
var approved: Bool
var doctorId: Int

private enum CodingKeys: String, CodingKey {
case approved
case doctorId
}

init(id: String, name: String, lastName: String, phoneNumber: String, email: String, imageURL:URL, approved:Bool, doctorId:Int) {
self.approved = approved
self.doctorId = doctorId
super.init(id: id, firstName: name, lastName: lastName, imageURL: imageURL)
}

required init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
self.approved = try values.decode(Bool.self, forKey: .approved)
self.doctorId = try values.decode(Int.self, forKey: .doctorId)
try super.init(from: decoder)
}

var description: String {
return "\(type(of: self)) - (\(self.firstName), \(self.lastName))" //superclass properties
}
}

struct PatientsList : Codable {
let patients: [Patient]
}

人类:

class Person: Codable {
let firstName: String
let lastName: String
let imageURL: URL?
let id: String

private enum CodingKeys: String, CodingKey {
case firstName
case lastName
case imageURL = "profileImgPath"
case id = "_id"
}

init(id: String, firstName: String, lastName: String, imageURL:URL) {
self.id = id
self.firstName = firstName
self.lastName = lastName
self.imageURL = imageURL
}

required init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
self.id = try values.decode(String.self, forKey: .id)
self.firstName = try values.decode(String.self, forKey: .firstName)
self.lastName = try values.decode(String.self, forKey: .lastName)
self.imageURL = try values.decodeIfPresent(URL.self, forKey: .imageURL)
}
}

这是我执行序列化的方式:

do {
let jsonData = try JSONSerialization.data(withJSONObject: JSON, options: [])
let decoder = JSONDecoder()
let patientList = try! decoder.decode(PatientsList.self, from: jsonData)
completionBlock(.success(patientList.patients))
} catch {
print(error.localizedDescription)
}

这里是 //在控制台中:po(patient.approved) 打印 false //在控制台中:表达式转储(患者)

▿ iHeal.Patient #0 ▿ super: iHeal.Person

firstName: "Sarah"

lastName: "Connor"

imageURL: nil

id: "5b367491052aaca6489e4805"

birthDate: "1980-06-10T00:00:00.000Z"

approved: false

doctorId: 12

print(patient.approved) //crash
print(patient.doctorId) // if I comment out previous line, this one crashes

Thread 1: EXC_BAD_ACCESS (code=1, address=0x200000002)

我可以提到的另一件奇怪的事情:没有在 Patient 上调用 description

print(patient) // prints plain: <Patient: 0x608000141c30>

杰森:

{
"patients": [
{
"approved": false,
"_id": "5b367491052aaca6489e4805",
"birthDate": "1980-06-10T00:00:00.000Z",
"doctorId": 12,
"firstName": "Sarah",
"lastName": "Connor"
},
{
"approved": false,
"drains": [],
"_id": "5b3674e5052aaca6489e66b3",
"__v": 0,
"birthDate": "1980-06-10T00:00:00.000Z",
"doctorId": 12,
"firstName": "Sarah",
"lastName": "Connor"
}
]
}

最佳答案

副本:Crash when accessing Object properties after decoding from JSON

从基类(即 Person)中删除了 Codable 一致性,但您仍然可以通过保持带有 docoder 的 init 方法从子类调用来解码基类成员。子类现在将符合 Codable。

关于json - JSON 序列化后子类属性崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51117700/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com