我有一个名为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
和unit
参数能够与初始化程序中提供的默认值不同。我还尝试为name
和unit
属性分配默认值,它们在结构内部声明,但在Parameter
协议所需的初始化程序之外。
有没有办法通过为name
和unit
参数提供默认值来初始化结构,只需要提供value
参数,而没有默认的初始化程序显示在Xcode的代码完成弹出窗口中?
我在Xcode的代码完成弹出窗口中出现两个初始值设定项的唯一问题是我不希望别人创建结构并提供除了我在初始化程序中提供的值之外的其他值Parameter
协议所要求的。
我意识到我可以在课堂上这样做,但我试图通过面向协议的编程和结构使我的思想正确。
答案 0 :(得分:0)
一种方法:
我们将所有变量更改为选项,并使用协议中的默认参数删除了初始化程序。如果unit
和value
的默认值始终,则客户端无需查看此初始值设定项。
protocol Parameter {
var name: String? { get }
var unit: Unit? { get }
var value: Double? { get }
//init(name: String, unit: Unit?, value: Double)
init(value: Double)
}
并将结构更改为:
struct Calcium: Parameter {
var name: String? = nil
var unit: Unit? = nil
var value: Double? = nil
init(value: Double) {
self.init(name: "Calcium", unit: UnitDispersion.partsPerMillion, value: value)
}
private init(name: String, unit: Unit, value: Double) {
self.name = name
self.unit = unit
self.value = value
}
}
在结构本身中,我们添加了私有初始化程序,它是从只需要一个参数的自定义初始化程序调用的。