[英]Subclassing swift generic decodable type
EDIT: As Rob Napier wrote , the problem exists in Xcode 9.2.编辑:正如Rob Napier 所写,问题存在于 Xcode 9.2 中。 In Xcode 9.3 the issue is no longer relevant.
在 Xcode 9.3 中,该问题不再相关。
My server json responses are all packed inside data
object:我的服务器 json 响应都打包在
data
对象中:
{
"data": {...}
}
So I have the following generic type to parse JSON:所以我有以下通用类型来解析 JSON:
class DataContainer<T: Decodable>: Decodable {
let data: T
init(data: T)
self.data = data
}
}
Most of the time it works fine, but there is one response in which I also need to parse included
field so I have created SpecificDataContainer
subclass:大多数情况下它工作正常,但有一个响应,我还需要解析
included
字段,因此我创建了SpecificDataContainer
子类:
class SpecificDataContainer: DataContainer<DataObject> {
let included: [IncludedObject]
init() {
included = []
super.init(data: DataObject(id: ""))
}
}
Implementation above gives me compiler error 'required' initializer 'init(from:)' must be provided by subclass of 'DataContainer'
.上面的实现给了我编译器错误
'required' initializer 'init(from:)' must be provided by subclass of 'DataContainer'
。
I implemented init(from:)
in the SpecificDataContainer
but compiler still gives me the same error.我在
SpecificDataContainer
实现了init(from:)
但编译器仍然给我同样的错误。
It seems I miss something obvious here.似乎我在这里错过了一些明显的东西。 What am I doing wrong?
我做错了什么? Here is my full code:
这是我的完整代码:
import Foundation
let jsonData = """
{
"data": {
"id": "some_id"
},
"included": [
{
"id": "some_id2"
}
]
}
""".data(using:.utf8)!
struct DataObject: Decodable {
let id: String
}
struct IncludedObject: Decodable {
let id: String
}
class DataContainer<T: Decodable>: Decodable {
let data: T
init(data: T) {
self.data = data
}
}
class SpecificDataContainer: DataContainer<DataObject> {
let included: [IncludedObject]
init() {
included = []
super.init(data: DataObject(id: ""))
}
required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
var includedArray = try container.nestedUnkeyedContainer(forKey: .included)
var includedObjects:[IncludedObject] = []
while !includedArray.isAtEnd {
let includedObject = try includedArray.decode(IncludedObject.self)
includedObjects.append(includedObject)
}
self.included = includedObjects
try super.init(from: decoder)
}
private enum CodingKeys: String, CodingKey {
case data = "data"
case included = "included"
}
}
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .iso8601
if let obj = try? decoder.decode(SpecificDataContainer.self, from: jsonData) {
print("object id \(obj.data.id)")
} else {
print("Fail!")
}
For some reason Xcode doesn't recognise the auto-generated init(from:)
in subclasses of Codable
(as Rob said it might be a bug).出于某种原因,Xcode 无法识别
Codable
子类中自动生成的init(from:)
(正如 Rob 所说,这可能是一个错误)。 Until Xcode 9.3 is out you can work around this issue by adding the initializer to the base class also:在 Xcode 9.3 发布之前,您还可以通过将初始化程序添加到基类来解决此问题:
class DataContainer<T: Decodable>: Decodable {
let data: T
enum CodingKeys: String, CodingKey {
case data
}
required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
data = try container.decode(T.self, forKey: .data)
}
This appears to be a bug in Xcode 9.2.这似乎是 Xcode 9.2 中的一个错误。 In 9.3b4 your code is fine.
在 9.3b4 中,您的代码很好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.