从 JSON 解码后访问对象属性时崩溃

Crash when accessing Object properties after decoding from JSON

我已将 JSON 值解码为对象。该对象看起来符合预期,但当我尝试访问它时,它是 属性.

let jsonData = try JSONSerialization.data(withJSONObject: JSON, options: [])
let decoder = JSONDecoder()
let doctor = try! decoder.decode(Doctor.self, from: jsonData)
let txt = "\(doctor.title). \(doctor.firstName) \(doctor.lastName)" // Runtime crash: (Thread 1: EXC_BAD_ACCESS (code=1, address=0x40))

Runtime crash: (Thread 1: EXC_BAD_ACCESS (code=1, address=0x40))

Class 人:

import UIKit

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

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

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

Class 医生:

import UIKit

class Doctor: Person {
    var title: String


    private enum CodingKeys: String, CodingKey {
        case title
    }

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

    required init(from decoder: Decoder) throws {

        let values = try decoder.container(keyedBy: CodingKeys.self)
        self.title = try values.decode(String.self, forKey: .title)
        try super.init(from: decoder)
    }

}

当我尝试你的代码时,它产生了同样的错误,经过调查,我发现这是 Swift 4.1 中的一个问题。您可以在下方查看,

https://bugs.swift.org/browse/SR-7090

目前可能的解决方案可能是如下略微重新安排,

从基础 class 中移除 Codable 一致性,即 Person 但您仍然可以通过将 init 方法与 [= 保持一致来解码基础 class 成员16=] 从 child classes 调用。 Child classes 现在将符合 Codable

class Person {
    let firstName: String
    let lastName: String
    let imageURL: URL

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

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

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

class Doctor: Person, Codable {
    var title: String


    private enum CodingKeys: String, CodingKey {
        case title
    }

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

    required override init(from decoder: Decoder) throws {

        let values = try decoder.container(keyedBy: CodingKeys.self)
        self.title = try values.decode(String.self, forKey: .title)
        try super.init(from: decoder)
    }  
}

现在,如果您有以下 json,它将按预期工作。

let json = """
{
  "title":"SomeTitle",
  "firstName": "SomeFirstName",
  "lastName": "SomeLastName",
  "profileImgPath": "urlPath"
}
""".data(using: .utf8)!

let decoder = JSONDecoder()
let doctor = try! decoder.decode(Doctor.self, from: json)
print(doctor.firstName)
print(doctor.lastName)
print(doctor.title)