gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 01:08:41 26 4
gpt4 key购买 nike

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

enter image description here

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))

类人:

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
}
}

类主任:

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

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

从基类(即 Person)中移除 Codable 一致性,但您仍然可以通过将 init 方法与 保持一致来解码基类成员docoder 从子类中调用。子类现在将符合 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)

关于ios - 从 JSON 解码后访问对象属性时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51004441/

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