gpt4 book ai didi

swift - 试图在 Swift 中使一个类可编码,但它的格式不正确?

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

我正在创建一个符合 codable 的类。

我有这个:

import Foundation

class Attribute : Decodable {

var number: Int16
var label: String?
var comments: String?

init(number:Int16, label:String?, comments:String?) {
self.number = number
self.label = label
self.comments = comments
}

// Everything from here on is generated for you by the compiler
required init(from decoder: Decoder) throws {
let keyedContainer = try decoder.container(keyedBy: CodingKeys.self)
number = try keyedContainer.decode(Int16.self, forKey: .number)
label = try keyedContainer.decode(String.self, forKey: .label)
comments = try keyedContainer.decode(String.self, forKey: .comments)
}

enum CodingKeys: String, CodingKey {
case number
case label
case comments
}

}

extension Attribute: Encodable {

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(number, forKey: .number)
try container.encode(label, forKey: .label)
try container.encode(comments, forKey: .comments)
}
}

这显然没问题。

我创建了一个 Attribute 的实例并使用以下代码对其进行编码:

  let newAttribute = Attribute.init(number:value.number, label:value.label, comments:value.shortcut)

然后我创建一个包含这些属性的数组并使用

对该数组进行编码
  let array = try JSONEncoder().encode(array)

这会将 Attribute 数组编码为 Data

然后我尝试使用以下方法将 Data 对象转换回 Attribute 数组:

let array = try JSONDecoder().decode(Attribute.self, from: data) as! Array<Attribute> 

我得到的第一个错误是:

Cast from 'Attribute' to unrelated type 'Array< Attribute>' always fails

如果我删除类型转换,我会在解码尝试时捕获此错误...

Optional("The data isn’t in the correct format.")

有什么想法吗?

最佳答案

您需要传入数组进行解码,不要传入数组元素类型,然后尝试将其强制转换为数组,这没有任何意义。 YourTypeArray<YourType>是两种不同且完全不相关的类型,因此您不能将一种类型转换为另一种类型,并且在调用 JSONDecoder.decode(_:from:) 时需要使用特定类型.

let array = try JSONDecoder().decode([Attribute].self, from: data)

顺便说一句,正如您在上一个问题中所指出的,无需手动编写 init(from:)encode(to:)方法或 CodingKeys enum因为对于您的简单类型,编译器可以为您自动合成所有这些。此外,如果您使用了 struct而不是 class ,您还将免费获得成员明智的初始化程序。

关于swift - 试图在 Swift 中使一个类可编码,但它的格式不正确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57496855/

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