mardi 23 juin 2020

Should I be using a switch statement instead of an if statement in Swift?

I am getting strange results when running this code. The buttons change a variable called genotype, and the function checkImage reads the genotype variable to change the image. Would it work better as a switch statement? If so, how can it be written?

Here is my code

//genotype
var ext = "ee"
var ago = "aa"
var gre = "gg"

func checkGenotype() -> String {
    ext + ago + gre

func checkPicture() {
    if (ext == "EE" || ext == "Ee") && ago == "aa" {
        baseLayer.image = #imageLiteral(resourceName: "black")
        base = String("black")
    }
    else if (ago == "Aa" || ago == "AA") && (ext == "EE" || ext == "Ee") {
        baseLayer.image = #imageLiteral(resourceName: "bay")
        base = String("bay")
    }
    else if ext == "ee" {
        baseLayer.image = #imageLiteral(resourceName: "chestnut")
        base = String("chestnut")
    }
    else if (gre == "Gg" || gre == "GG") {
        baseLayer.image = #imageLiteral(resourceName: "grey")
        base = String("grey")
    }

Aucun commentaire:

Enregistrer un commentaire