With performance in consideration which is faster in Swift?
For example, here is a code snippet which does the same thing but with switch the code is more succint.
// switch
switch someString {
case "world":
print(1)
case "hello":
print(2)
case let s where s.contains("haha"):
print(3)
default:
print(4)
}
// if-elseif-else
if someString == "world" {
print(1)
} else if someString == "hello" {
print(2)
} else if someString.contains("haha") {
print(3)
} else {
print(4)
}
Here's also another example where switch and if-else can also do the same thing.
Aucun commentaire:
Enregistrer un commentaire