Why would you want to use Enum with dictionary values, because you get the benefit of the features of using an enum (closed, consistent and complete list) with the possibility of having associated values for any additional functionality (icons, labels, text, accessibility labels, etc.).
There maybe a better or different way to get the same effect, so consider
enum VolumeSetting: String, CaseIterable, Identifiable {
case loud = "Loud"
case normal = "Normal"
case soft = "Soft"
var id: String { self.rawValue }
}
func volumeValue(for volume: VolumeSetting) -> Float {
// do not want to increase dictionary size either deliberately or by accident
guard VolumeSetting.allCases.contains(volume) else { return 0.5 }
let volumeValue: [VolumeSetting: Float] = [
.loud: 0.75,
.normal: 0.5,
.soft: 0.25
]
return volumeValue[volume]!
}
func volumeIcon (for volume: VolumeSetting) -> String {
// do not want to increase dictionary size either deliberately or by accident
guard VolumeSetting.allCases.contains(volume) else { return "speaker.wave.2" }
let volumeIcon: [VolumeSetting: String] = [
.loud: "speaker.wave.3",
.normal: "speaker.wave.2",
.soft: "speaker.wave.1"
]
return volumeIcon[volume]!
}
Assuming that audioPlayer has already been created as an AVAudioPlayer type instance, you can write
audioPlayer?.setVolume(volumeValue(for: volumeSetting), fadeDuration: 3)
and 'volumeSetting' is defined as a VolumeSetting enum.
playAlarmSound(volumeSetting)
and
Button("\(Image(systemName: volumeIcon(for: volumeSetting)))") {
// more code here
}
.accessibilityElement()
.accessibilityLabel("Volume")
.accessibilityHint("Volume is \(volumeSetting.rawValue.description)")