我有一个名为的协议Parameter
:
protocol Parameter {
var name: String { get }
var unit: Unit? { get }
var value: Double { get }
init(name: String, unit: Unit?, value: Double)
}
我还有 16 个结构符合Parameter
:
struct Calcium: Parameter {
var name: String
var unit: Unit?
var value: Double
init(name: String = "Calcium", unit: Unit? = UnitDispersion.partsPerMillion, value: Double) {
self.name = name
self.unit = unit
self.value = value
}
}
struct Alkalinity: Parameter {
var name: String
var unit: Unit?
var value: Double
init(name: String = "Alkalinity", unit: Unit? = Unit(symbol: "ppm CaCO3"), value: Double) {
self.name = name
self.unit = unit
self.value = value
}
}
struct Salinity: Parameter {
var name: String
var unit: Unit?
var value: Double
init(name: String = "Salinity", unit: Unit? = Unit(symbol: "SG"), value: Double) {
self.name = name
self.unit = unit
self.value = value
}
}
struct Temperature: Parameter {
var name: String
var unit: Unit?
var value: Double
init(name: String = "Temperature", unit: Unit? = UnitTemperature.fahrenheit, value: Double) {
self.name = name
self.unit = unit
self.value = value
}
}
struct PH: Parameter {
var name: String
var unit: Unit?
var value: Double
init(name: String = "pH", unit: Unit? = nil, value: Double) {
self.name = name
self.unit = unit
self.value = value
}
}
struct Magnesium: Parameter {
var name: String
var unit: Unit?
var value: Double
init(name: String = "Magnesium", unit: Unit? = UnitDispersion.partsPerMillion, value: Double) {
self.name = name
self.unit = unit
self.value = value
}
}
struct Phosphate: Parameter {
var name: String
var unit: Unit?
var value: Double
init(name: String = "Phosphate", unit: Unit? = UnitDispersion.partsPerMillion, value: Double) {
self.name = name
self.unit = unit
self.value = value
}
}
struct Ammonia: Parameter {
var name: String
var unit: Unit?
var value: Double
init(name: String = "Ammonia", unit: Unit? = UnitDispersion.partsPerMillion, value: Double) {
self.name = name
self.unit = unit
self.value = value
}
}
struct Silica: Parameter {
var name: String
var unit: Unit?
var value: Double
init(name: String = "Silica", unit: Unit? = UnitDispersion.partsPerMillion, value: Double) {
self.name = name
self.unit = unit
self.value = value
}
}
struct Iodine: Parameter {
var name: String
var unit: Unit?
var value: Double
init(name: String = "Iodine", unit: Unit? = UnitDispersion.partsPerMillion, value: Double) {
self.name = name
self.unit = unit
self.value = value
}
}
struct Nitrate: Parameter {
var name: String
var unit: Unit?
var value: Double
init(name: String = "Nitrate", unit: Unit? = UnitDispersion.partsPerMillion, value: Double) {
self.name = name
self.unit = unit
self.value = value
}
}
struct Nitrite: Parameter {
var name: String
var unit: Unit?
var value: Double
init(name: String = "Nitrite", unit: Unit? = UnitDispersion.partsPerMillion, value: Double) {
self.name = name
self.unit = unit
self.value = value
}
}
struct Strontium: Parameter {
var name: String
var unit: Unit?
var value: Double
init(name: String = "Strontium", unit: Unit? = UnitDispersion.partsPerMillion, value: Double) {
self.name = name
self.unit = unit
self.value = value
}
}
struct ORP: Parameter {
var name: String
var unit: Unit?
var value: Double
init(name: String = "ORP", unit: Unit? = UnitDispersion.partsPerMillion, value: Double) {
self.name = name
self.unit = unit
self.value = value
}
}
struct Boron: Parameter {
var name: String
var unit: Unit?
var value: Double
init(name: String = "Boron", unit: Unit? = UnitDispersion.partsPerMillion, value: Double) {
self.name = name
self.unit = unit
self.value = value
}
}
struct Iron: Parameter {
var name: String
var unit: Unit?
var value: Double
init(name: String = "Iron", unit: Unit? = UnitDispersion.partsPerMillion, value: Double) {
self.name = name
self.unit = unit
self.value = value
}
}
如您所见,所有结构都实现了相同的初始化程序。在初始化程序中,我为Parameter
协议所需的三个参数中的两个提供了默认值,这给了我以下初始化程序:
let calcium = Calcium(value: Double)
我还得到了结构创建的默认初始化程序:
let calcium = Calcium(name: String, unit: Unit?, value: Double)
当我创建结构的实例时,两个初始化器都出现在 Xcode 的代码完成弹出窗口中。但是,我不希望name
andunit
参数能够与初始化程序中提供的默认值不同。我还尝试为在结构内部但在协议所需的初始化程序之外声明它们的name
和属性分配默认值。unit
Parameter
name
有没有办法通过为和unit
参数提供默认值来初始化结构,只value
需要提供参数,而不在 Xcode 的代码完成弹出窗口中显示默认初始化程序?
我对让两个初始化程序都出现在 Xcode 的代码完成弹出窗口中的唯一担心是,我不希望其他人创建结构并提供除Parameter
协议所需的初始化程序中提供的值之外的值。
我意识到我可以通过一个类来做到这一点,但我试图通过面向协议的编程和使用结构来让我的想法正确。