gpt4 book ai didi

struct - 有没有办法隐藏 Swift Struct 的默认初始化程序?

转载 作者:行者123 更新时间:2023-12-02 07:43:17 24 4
gpt4 key购买 nike

我有一个名为 Parameter 的协议(protocol):

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 所需的三个参数中的两个提供默认值。协议(protocol),它为我提供了以下初始化程序:

let calcium = Calcium(value: Double)

我还获得了结构创建的默认初始值设定项:

let calcium = Calcium(name: String, unit: Unit?, value: Double)

当我创建结构体的实例时,两个初始化程序都出现在 Xcode 的代码完成弹出窗口中。但是,我不想要 nameunit参数可以是与初始化程序中提供的默认值不同的任何值。我还尝试为 name 分配默认值和unit属性,其中它们在结构内部但在 Parameter 所需的初始化程序之外声明。协议(protocol)。

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

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

我意识到我可以通过一个类来做到这一点,但我正在尝试通过面向协议(protocol)的编程和使用结构来纠正我的想法。

最佳答案

一种方法:

我们将所有变量更改为可选变量,并从协议(protocol)中删除了带有默认参数的初始化程序。如果 unitvalue始终使用默认值,则客户端无需查看此初始值设定项。

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
}

}

在结构本身中,我们添加了私有(private)初始值设定项,该初始值设定项是从仅需要一个参数的自定义初始值设定项调用的。

并且您在 Xcode 完成弹出窗口中将只有一个初始值设定项。 image

关于struct - 有没有办法隐藏 Swift Struct 的默认初始化程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39134797/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com