You need another variable in the Setting struct that is going to hold the currently selected value.
For example: var currentVolume = 50
If you'd like to be able to lock the currentVolume variable so that it can only be set between your range of 0 to 100, one possible implementation would be to make the currentVolume variable private to the Setting struct and create a function that sets the volume based on a given input, or functions that increment/decrement the volume based on some step, like this:
struct Setting {
private var volumeRange = (0...100)
// We've made it private(set) so that we can read and print it, but not change it,
// because we want it to only be changed by the functions we created,
// which functions will make sure it stays within range.
private(set) var currentVolume = 10
private var step = 5
enum Display: String {
case colorful = "Color"
case blackwhite = "Black&White"
}
mutating func setVolume(to newVolume: Int) {
// Here we are checking if newVolume is within the range of 0 to 100
if volumeRange.contains(newVolume) {
currentVolume = newVolume
// If it is not, here we are checking if newVolume is above 100,
// and if so, we set it to our upper range, which is 100
} else if volumeRange.upperBound < newVolume {
currentVolume = volumeRange.upperBound
// And finally we are checking if newVolume is below 0,
// and if so, we set it to our lower range, which is 0
} else {
currentVolume = volumeRange.lowerBound
}
}
mutating func incrementVolume() {
// Here we are creating a newVolume variable that will have
// our currentVolume incremented by the step variable (which in this case is 5)
let newVolume = currentVolume + step
// Here we are checking if the new volume is within our specifed range
// For example, if currentVolume was 100, the newVolume would now be 105,
// which is not the in range, so we would not want to change currentVolume
if newVolume <= volumeRange.upperBound {
currentVolume = newVolume
}
}
mutating func decrementVolume() {
// Here we are creating a newVolume variable that will have
// our currentVolume decremented by the step variable (which in this case is 5)
let newVolume = currentVolume - step
// Here we are checking if the new volume is within our specifed range
// For example, if currentVolume was 0, the newVolume would now be -5,
// which is not the in range, so we would not want to change currentVolume
if newVolume >= volumeRange.lowerBound {
currentVolume = newVolume
}
}
}
var setting = Setting()
print(setting.currentVolume) // 10
setting.incrementVolume()
print(setting.currentVolume) // 15
setting.setVolume(to: 105)
print(setting.currentVolume) // 100
setting.incrementVolume()
print(setting.currentVolume) // 100
setting.decrementVolume()
print(setting.currentVolume) // 95
setting.setVolume(to: -5)
print(setting.currentVolume) // 0
setting.decrementVolume()
print(setting.currentVolume) // 0
Your volume variable var volume = (0...100) is of type ClosedRange<Int>. It cannot hold a specific value, what it holds is a range with a lower and an upper bound. You can double-check this on any variable by holding down the option key ⌥ and then clicking on the variable name (in this case clicking on 'volume'). You can find information on the ClosedRange type and others in Apple's documentation here: https://developer.apple.com/documentation/swift/closedrange