gpt4 book ai didi

ios - 快速输入属性?

转载 作者:搜寻专家 更新时间:2023-10-31 22:47:47 25 4
gpt4 key购买 nike

在类型属性的 Apple 文档中,给出了以下代码:

struct AudioChannel {
static let thresholdLevel = 10
static var maxInputLevelForAllChannels = 0
var currentLevel: Int = 0 {
didSet {
if currentLevel > AudioChannel.thresholdLevel {
// cap the new audio level to the threshold level
currentLevel = AudioChannel.thresholdLevel
}
if currentLevel > AudioChannel.maxInputLevelForAllChannels {
// store this as the new overall maximum input level
AudioChannel.maxInputLevelForAllChannels = currentLevel
}
}
}
}

类型属性的定义方式类似于 C 语言的“静态”变量。在上面的例子中,声明类型属性有什么好处,如果不声明它们会有什么影响或有什么不同?

最佳答案

顾名思义,类型变量是整个类型的变量,与实例变量相反,实例变量是每个实例的变量。

这意味着,就您发布的代码而言,您创建的每个 AudioChannel 都将具有相同的 thresholdLevelmaxInputLevelForAllChannels 值.当某些东西改变了这些变量时,所有实例都可以访问新值。

关于ios - 快速输入属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33992351/

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