lundi 25 juillet 2016

How do I make my if statement more efficient?

Writing some swift code for an app and whilst it works fine, I can't help but think it's ugly and can be improved upon, I'm still new and always looking for ways to improve my code so any help is appreciated. In context it's for a slider and a couple of labels.

    var value1 = Int (sender.value)

    value1text.text = "\(value1)"

    var value2:Int

    if value1 <= 5 {
        endText.text = "\(7 - value1)"
    } else if value1 == 6 {
        value1 = 2
        value2 = 6
        endText.text = "\(value1)/\(value2)"
    } else if value1 == 7 {
        value1 = 2
        value2 = 6
        endText.text = "\(value1)/\(value2)"
    } else if value1 == 8 {
        value1 = 2
        value2 = 6
        endText.text = "\(value1)/\(value2)"
    } else if value1 == 9 {
        value1 = 2
        value2 = 6
        endText.text = "\(value1)/\(value2)"
    } else if value1 == 10 {
        value1 = 2
        value2 = 6
        endText.text = "\(value1)/\(value2)"
    }

My vars are better named in my app, just renamed for easier reading (hopefully)

Thanks!

Aucun commentaire:

Enregistrer un commentaire