子类化 swift 泛型可解码类型
Subclassing swift generic decodable type
编辑: 作为,问题存在于Xcode 9.2。在 Xcode 9.3 中,该问题不再相关。
我的服务器 json 响应全部打包在 data
对象中:
{
"data": {...}
}
所以我有以下通用类型来解析 JSON:
class DataContainer<T: Decodable>: Decodable {
let data: T
init(data: T)
self.data = data
}
}
大多数时候它工作正常,但有一个响应我还需要解析 included
字段所以我创建了 SpecificDataContainer
子类:
class SpecificDataContainer: DataContainer<DataObject> {
let included: [IncludedObject]
init() {
included = []
super.init(data: DataObject(id: ""))
}
}
上面的实现给我编译器错误 'required' initializer 'init(from:)' must be provided by subclass of 'DataContainer'
。
我在 SpecificDataContainer
中实现了 init(from:)
,但编译器仍然给我同样的错误。
看来我在这里漏掉了一些明显的东西。我究竟做错了什么?这是我的完整代码:
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!")
}
这似乎是 Xcode 9.2 中的错误。在 9.3b4 中你的代码没问题。
出于某种原因 Xcode 无法识别 Codable
的子 class 中的 auto-generated init(from:)
(正如 Rob 所说,它可能是漏洞)。在 Xcode 9.3 发布之前,您可以通过将初始化器添加到基础 class 来解决此问题:
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)
}
编辑: 作为
我的服务器 json 响应全部打包在 data
对象中:
{
"data": {...}
}
所以我有以下通用类型来解析 JSON:
class DataContainer<T: Decodable>: Decodable {
let data: T
init(data: T)
self.data = data
}
}
大多数时候它工作正常,但有一个响应我还需要解析 included
字段所以我创建了 SpecificDataContainer
子类:
class SpecificDataContainer: DataContainer<DataObject> {
let included: [IncludedObject]
init() {
included = []
super.init(data: DataObject(id: ""))
}
}
上面的实现给我编译器错误 'required' initializer 'init(from:)' must be provided by subclass of 'DataContainer'
。
我在 SpecificDataContainer
中实现了 init(from:)
,但编译器仍然给我同样的错误。
看来我在这里漏掉了一些明显的东西。我究竟做错了什么?这是我的完整代码:
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!")
}
这似乎是 Xcode 9.2 中的错误。在 9.3b4 中你的代码没问题。
出于某种原因 Xcode 无法识别 Codable
的子 class 中的 auto-generated init(from:)
(正如 Rob 所说,它可能是漏洞)。在 Xcode 9.3 发布之前,您可以通过将初始化器添加到基础 class 来解决此问题:
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)
}