I am creating an app that needs to change a label based on an integer value, there are ten different labels and 13 different integer options. Does anyone know of a way to convert it without using a bunch of if statements? Also the original value does need to be an int as it is assigned based on a UIPicker.
if class1Grade == 1 {
class1LetterGrade = "A "
} else if class1Grade == 2 {
class1LetterGrade = "A"
}
It needs to do conversion along these lines. Just wanted to check if there was a way that could save me from writing an extra few hundred lines of code.
CodePudding user response:
You can use a Dictionary:
func letterGrade(_ grade: Int) -> String? {
return [
1: "A ",
2: "A",
3: "B",
][grade]
}
letterGrade(1) // "A "
letterGrade(2) // "A"
letterGrade(7) // nil