From Swift documentation:
Typically, you use the if statement to evaluate simple conditions with only a few possible outcomes. The switch statement is better suited to more complex conditions with multiple possible permutations and is useful in situations where pattern matching can help select an appropriate code branch to execute.
I'm trying to decide on if I should use the switch or if/else statements based on if I have a complex condition. So my question is, is my condition complex or not.
Here is an example of what I have:
var sampleVar = Measurement(value: amount, unit: UnitLength.megameters)
if(type == "inches"){
//do something
}
else if...
I have between 5 to 15 possible conditions I'm checking for so would that make it complex enough to justify using a switch statement? Or is complexity based on the condition and not how many conditions there are?
Aucun commentaire:
Enregistrer un commentaire