swift 如何从数组中得到数值?

fdx2calv  于 2022-11-28  发布在  Swift
关注(0)|答案(2)|浏览(137)

这是必要的实现音量的电视。struct设置var音量范围0... 100。如何写打印音量水平?
另一个问题:有没有可能更好地使电视的品牌和型号在同一领域?var tvName

struct Setting {
    var volume = (0...100)
    enum Display: String {
         case colorful = "Color"
         case blackwhite = "Black&White"
    }
}
class Tv {
    var tvName = (firm: "", model: 0)
    var isActive = Bool()
    enum Channels: String {
        case news = "News"
        case serials = "Serials"
        case horror = "Horror"
        case comedy = "Comedy"
        case history = "History"
        case cartoons = "Cartoons"
    }
    func tvStatus() {
        if isActive == true {
            print("The TV \(tvName.0) \(tvName.1) is on and shows the channel \(channelOldTv), display: \(display), volume: ")
        } else {
            print("TV is off")
        }
    }
}
let oldTv = Tv()
let channelOldTv = Tv.Channels.serials.rawValue
let display = Setting.Display.colorful.rawValue
oldTv.tvName.firm = "LG"
oldTv.tvName.model = 6643
oldTv.isActive = true
oldTv.tvStatus()
uajslkp6

uajslkp61#

从我的Angular 来看,您需要稍微调整一下代码,因为数据类型没有很好地实现。我将这样编写代码:

class TV {
    let type: TVType
    let isActive: Bool
    let setting: TVSetting
    let channel: TVChannel
    
    
    init(type: TVType, isActive: Bool, setting: TVSetting, channel: TVChannel) {
        self.type = type
        self.isActive = isActive
        self.setting = setting
        self.channel = channel
    }
    
    func printTVStatus() {
        if self.isActive {
            print("The TV \(self.type.brand) \(self.type.model) is on and shows the channel \(self.channel.rawValue), display: \(self.setting.display.rawValue), volume: \(self.setting.volume)")
        } else {
            print("TV is off")
        }
    }
}

struct TVSetting {
    let volumeRange: (ClosedRange<Int>) = (0...100)
    let volume: Int
    let display: Display
    
    enum Display: String {
         case colorful = "Color"
         case blackwhite = "Black&White"
    }
    
    
    init(volume: Int, display: Display) {
        self.volume = self.setVolumeValue(volume)
        self.display = display
    }
    
    private func isVolumeValueValid(_ value: Int) -> Bool {
        return self.volumeRange.contains(value)
    }
    
    private func setVolumeValue(_ value: Int) -> Int {
        if self.isVolumeValueValid(value) {
            return value
        } else {
            // handle the caae when the value is not a valid one, maybe set a default one and throw an error, or print a message
            return 0
        }
    }
}

enum TVChannel: String {
    case news = "News"
    case serials = "Serials"
    case horror = "Horror"
    case comedy = "Comedy"
    case history = "History"
    case cartoons = "Cartoons"
}

struct TVType {
    let brand: String
    let model: Int // Maybe it would be better to change it to String, because you might have models which start with 034 etc
}


class MyClass {
    
    let oldTv = TV(type: TVType(brand: "BrandName", model: 233), isActive: true, setting: TVSetting(volume: 20, display: .blackwhite), channel: .cartoons)
    
    func doSomething() {
        self.oldTv.printTVStatus()
    }
    
 
}

正如上面提到的,变量volume必须保存值。volumeRange表示volume必须包含的范围。为此,在初始化设置时,必须对volume字段进行一些验证。

pftdvrlh

pftdvrlh2#

您需要在Setting结构中使用另一个变量来保存当前选定的值。
例如:var currentVolume = 50
如果您希望能够锁定currentVolume变量,使其只能在0到100之间设置,一种可能的实现方式是将currentVolume变量私有化为Setting结构体,并创建一个基于给定输入设置音量的函数,或者创建一个基于某个步长递增/递减音量的函数,如下所示:

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

您的卷变量var volume = (0...100)的类型为ClosedRange<Int>。它不能保存特定值,它包含的是一个具有上下限的范围。你可以通过按住option键****然后点击变量名来对任何变量进行双重检查(在本例中,点击“音量”)。您可以在Apple的文档中找到有关ClosedRange类型和其他类型的信息,请访问:https://developer.apple.com/documentation/swift/closedrange

相关问题