简体   繁体   中英

Multiple intervals in single switch-case using tuple Swift 3

I am new to programming and I am trying to do a switch with multiple conditions. I have an label that takes temperature in Celsius or Fahrenheit in dependence of which one is set, I need to take the value and attribute some settings based on the temperature interval. How can I manage that in one switch (if that is possible), I know how to do that using 4 switches, but it's very long code... I tried smth like this :

        let todayStringComponent = Double(tempLabel.text!)
        let fahrStringComponent = Double(tempLabel.text!)
        let yesterdayStringComponent = Double(yesterdayLabel.text!)
        switch (celsUnitButtonBool, todayStringComponent!) {
        case (true?, -100 ... -40) :
            //  gradientImageOutlet.image = UIImage(named: "thermo_01.png")!
            if UIDevice.current.userInterfaceIdiom == .pad
            {
                temperatureViewCenterAxisCnstr.constant = 315
                gradientHeight.constant = 0
                animateChanges()
            } else if  UIDevice.current.userInterfaceIdiom == .phone
            {
                temperatureViewCenterAxisCnstr.constant = 315
                gradientHeight.constant = 470

                animateChanges()
            }
        case (true?, -39 ... -26):
            //   gradientImageOutlet.image = UIImage(named: "thermo_02.png")!
            if UIDevice.current.userInterfaceIdiom == .pad
            {
                temperatureViewCenterAxisCnstr.constant = 275
                gradientHeight.constant = 75

                animateChanges()
            } else if  UIDevice.current.userInterfaceIdiom == .phone
            {
                temperatureViewCenterAxisCnstr.constant = 315
                gradientHeight.constant = 470

                animateChanges()
            }   default:
            print("WTH")
        }

Is it possible to manage that in one switch so it can do the following:

1)check if it's Fahr or Cels,

2)If it's Cels to check the intervals for Celsius and do stuff

3)If it's Fahr to check intervals for Fahrenheit and do stuff

4)And for yesterdayLabel (with same intervals for TodayLabel) do to other stuff.

Hope somebody will understand what I mean. Thank you.

Encapsulating some logic into an enum would simplify the client code to the following:

let celsUnitButtonBool = true
let todayStringComponent = -50

let sample = Temperature(cellButtonIsPressed: celsUnitButtonBool,
                         value: todayStringComponent)

switch (sample, sample.value) {
case (.Fahrenheit, -100 ... -40):
    break
case (.Celsius, -39 ... -26):
    break
default:
    break
}

The implementation of Temperature is rather straightforward.

enum Temperature {
    case Fahrenheit(value: Int)
    case Celsius(value: Int)
}

extension Temperature {
    init(cellButtonIsPressed: Bool, value: Int) {
        if cellButtonIsPressed {
            self = .Celsius(value: value)
        } else {
            self = .Fahrenheit(value: value)
        }
    }

    var value: Int {
        switch self {
        case .Fahrenheit(value: let value),
             .Celsius(value: let value):
            return value
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM