Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/111.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 解码子类时忽略超类属性_Ios_Json_Swift_Swift4_Codable - Fatal编程技术网

Ios 解码子类时忽略超类属性

Ios 解码子类时忽略超类属性,ios,json,swift,swift4,codable,Ios,Json,Swift,Swift4,Codable,我试图创建继承的数据模型,以便使用jsondeconder解析它 class FirstClass : Codable { let firstClassProperty: Int final let arrayOfInts: [Int] } class SecondClass : FirstClass { let secondClassProperty1: Int let secondClassProperty2: Int private enum C

我试图创建继承的数据模型,以便使用
jsondeconder
解析它

class FirstClass : Codable {
    let firstClassProperty: Int
    final let arrayOfInts: [Int]
}

class SecondClass : FirstClass {
    let secondClassProperty1: Int
    let secondClassProperty2: Int

    private enum CodingKeys : String, CodingKey {
        case secondClassProperty1, secondClassProperty2
    }

    required init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)

        secondClassProperty1 = try container.decode(Int.self, forKey: .secondClassProperty1)
        secondClassProperty2 = try container.decode(Int.self, forKey: .secondClassProperty2)

        try super.init(from: decoder)
    }
}
我将此JSON用于
FirstClass

{
    "firstClassProperty":  123,
    "arrayOfInts": [
        123
    ]
}
{
  "firstClassProperty": {},
  "secondClassProperty1": {},
  "secondClassProperty2": {}
}
这是第二类的:

{
    "firstClassProperty":  123,
    "arrayOfInts": [
        123
    ]
}
{
  "firstClassProperty": {},
  "secondClassProperty1": {},
  "secondClassProperty2": {}
}
如果关键字
final
在这种情况下不起作用,我如何在我的子类中去掉
arrayFints
,但将其放在超类中


给你。谢谢你的回答

一个快速破解方法是将其声明为可选。例如:

class FirstClas: Codable {
    let firstClassProperty: Int
    final let arrayOfInts: [Int]?
}
这将自动解决缺少的
arrayFints
问题

手动解决方案。另一种解决方案是自己实现
Decodable
协议-就像您在
SecondClass
中所做的那样-并使用对
arrayFints
进行解码(否则使用默认值)


超类解码。顺便说一下,将
解码器
转发到超类的推荐方法是使用
superDecoder()
方法:

...
let superDecoder = try container.superDecoder()
try super.init(from: superDecoder)

一个快速的攻击是将其声明为可选。例如:

class FirstClas: Codable {
    let firstClassProperty: Int
    final let arrayOfInts: [Int]?
}
这将自动解决缺少的
arrayFints
问题

手动解决方案。另一种解决方案是自己实现
Decodable
协议-就像您在
SecondClass
中所做的那样-并使用对
arrayFints
进行解码(否则使用默认值)


超类解码。顺便说一下,将
解码器
转发到超类的推荐方法是使用
superDecoder()
方法:

...
let superDecoder = try container.superDecoder()
try super.init(from: superDecoder)

您可以这样使用:

class FirstClass : Codable {
    let firstClassProperty: Int
    final let arrayOfInts: [Int]?
    required init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        firstClassProperty = try container.decode(Int.self, forKey: .firstClassProperty)
        arrayOfInts = try container.decodeIfPresent([Int].self, forKey: .arrayOfInts)
    }
}

您可以这样使用:

class FirstClass : Codable {
    let firstClassProperty: Int
    final let arrayOfInts: [Int]?
    required init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        firstClassProperty = try container.decode(Int.self, forKey: .firstClassProperty)
        arrayOfInts = try container.decodeIfPresent([Int].self, forKey: .arrayOfInts)
    }
}

如果不总是需要,
ArrayFints
为什么在基类中?这里似乎不需要层次结构。如果不总是需要,
ArrayFints
为什么在基类中?这里似乎不需要层次结构。如果属性已标记为可选,我认为自动生成的代码在这里就足够了——顺便说一句,应该与您手动编写的代码非常相似;)如果属性已经标记为可选,我认为自动生成的代码在这里就足够了——顺便说一句,应该与您手动编写的代码非常相似;)@LeoDabus我是我自己,但是,如果没有来自好孤独者的进一步信息,我真的不能像你说的那样,直截了当地杀了他的课,哈哈;)他可能需要在一个有一整套未知设计约束的大得多的应用程序中使用这种功能…@LeoDabus不是真的。在我的回答中使用了一个特定的
superDecoder()
API,它向我发出信号,苹果公司明确地将其设计为供类使用。可能有一些遗漏的地方,但我的意图似乎很清楚…@LeoDabus…苹果实际上提供了一个:-)我不会这么说。这个问题主要集中在忽略超类属性上(而不是像其他问题那样全面讨论继承)。OP真的不可能仅仅通过看另一个更大的问题来破解一个解决方案,IMHO…@LeoDabus我是我自己,但是,如果没有来自好孤独者的进一步信息,我真的不能像你说的那样直接杀死他的班级,哈哈;)他可能需要在一个有一整套未知设计约束的大得多的应用程序中使用这种功能…@LeoDabus不是真的。在我的回答中使用了一个特定的
superDecoder()
API,它向我发出信号,苹果公司明确地将其设计为供类使用。可能有一些遗漏的地方,但我的意图似乎很清楚…@LeoDabus…苹果实际上提供了一个:-)我不会这么说。这个问题主要集中在忽略超类属性上(而不是像其他问题那样全面讨论继承)。OP真的不可能仅仅通过看另一个更大的问题来破解一个解决方案,IMHO。。。