初始化期间的可编码默认值
Codable default values during initialization
我是 Swift 的新手,我正在为我的项目研究功能标志概念,但我一直坚持使用 codable 作为默认标志值。目前我的代码看起来像这样
import Foundation
class KillSwitches: Codable {
public enum CodingKeys: String, CodingKeys {
case featureOne
case featureTwo
case featureThree
}
let featureOne: Bool = true
let featureTwo: Bool = true
let featureThree: Bool = false
}
我有内部助手 classes,它有助于对 json 文件中的所有值进行编码和解码,这就是此处未明确提及的原因。在此实现之前,我没有任何默认值,并且使用结构从远程配置文件中读取所有内容,该文件工作正常。现在,如果无法访问远程配置文件,我将在下一步为我的功能设置默认值。
我原以为我可以初始化这个 class 所以我会得到一个 class 的默认对象,就像我从远程文件读取时得到的一样。
如果不通过 init(from decoder:),我无法实例化此 class。我什至尝试过
KillSwitches.init(from: KillSwitches.self)
也不起作用,我得到类型不符合预期类型解码器。
我的 Json 看起来像这样
{
"featureOne" : false,
"featureTwo" : true,
"featureThree" : true
}
非常感谢任何解决此问题的指导/指示。
一旦您符合 Encodable
,就好像您的 class 已经显式声明了一个 encode(to:)
方法和一个 init(from:)
初始化程序。
通过声明带参数的初始化程序,您会立即失去编译器在所有属性都具有默认值时为您生成的默认(无参数)初始化程序。这就是为什么你不能做 KillSwitches()
。这在 documentation:
中说明
Swift provides a default initializer for any structure or class that
provides default values for all of its properties and does not provide
at least one initializer itself. The default initializer simply
creates a new instance with all of its properties set to their default
values.
KillSwitches
已经有一个 init(from:)
初始化器,所以 Swift 不提供默认初始化器。
你只需要自己添加无参数初始化器:
class KillSwitches: Codable {
public enum CodingKeys: String, CodingKey {
case featureOne
case featureTwo
case featureThree
}
let featureOne: Bool = true
let featureTwo: Bool = true
let featureThree: Bool = false
init() { }
}
然后你可以做:
let defaultKillSwitches = KillSwitches()
如果你想要默认值。
我是 Swift 的新手,我正在为我的项目研究功能标志概念,但我一直坚持使用 codable 作为默认标志值。目前我的代码看起来像这样
import Foundation
class KillSwitches: Codable {
public enum CodingKeys: String, CodingKeys {
case featureOne
case featureTwo
case featureThree
}
let featureOne: Bool = true
let featureTwo: Bool = true
let featureThree: Bool = false
}
我有内部助手 classes,它有助于对 json 文件中的所有值进行编码和解码,这就是此处未明确提及的原因。在此实现之前,我没有任何默认值,并且使用结构从远程配置文件中读取所有内容,该文件工作正常。现在,如果无法访问远程配置文件,我将在下一步为我的功能设置默认值。
我原以为我可以初始化这个 class 所以我会得到一个 class 的默认对象,就像我从远程文件读取时得到的一样。
如果不通过 init(from decoder:),我无法实例化此 class。我什至尝试过
KillSwitches.init(from: KillSwitches.self)
也不起作用,我得到类型不符合预期类型解码器。
我的 Json 看起来像这样
{
"featureOne" : false,
"featureTwo" : true,
"featureThree" : true
}
非常感谢任何解决此问题的指导/指示。
一旦您符合 Encodable
,就好像您的 class 已经显式声明了一个 encode(to:)
方法和一个 init(from:)
初始化程序。
通过声明带参数的初始化程序,您会立即失去编译器在所有属性都具有默认值时为您生成的默认(无参数)初始化程序。这就是为什么你不能做 KillSwitches()
。这在 documentation:
Swift provides a default initializer for any structure or class that provides default values for all of its properties and does not provide at least one initializer itself. The default initializer simply creates a new instance with all of its properties set to their default values.
KillSwitches
已经有一个 init(from:)
初始化器,所以 Swift 不提供默认初始化器。
你只需要自己添加无参数初始化器:
class KillSwitches: Codable {
public enum CodingKeys: String, CodingKey {
case featureOne
case featureTwo
case featureThree
}
let featureOne: Bool = true
let featureTwo: Bool = true
let featureThree: Bool = false
init() { }
}
然后你可以做:
let defaultKillSwitches = KillSwitches()
如果你想要默认值。