3

我有一个名为的协议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 的代码完成弹出窗口中。但是,我不希望nameandunit参数能够与初始化程序中提供的默认值不同。我还尝试为在结构内部但在协议所需的初始化程序之外声明它们的name和属性分配默认值。unitParameter

name有没有办法通过为和unit参数提供默认值来初始化结构,只value需要提供参数,而不在 Xcode 的代码完成弹出窗口中显示默认初始化程序?

我对让两个初始化程序都出现在 Xcode 的代码完成弹出窗口中的唯一担心是,我不希望其他人创建结构并提供除Parameter协议所需的初始化程序中提供的值之外的值。

我意识到我可以通过一个类来做到这一点,但我试图通过面向协议的编程和使用结构来让我的想法正确。

4

1 回答 1

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
    }

}

在结构本身中,我们添加了私有初始化器,它从自定义初始化器中调用,只需要一个参数。

而且您在 Xcode 完成弹出窗口中将只有一个初始化程序。 图片

于 2016-10-12T09:16:32.027 回答