I came to Objective-C from a VB.NET background where the switch
statement is Select Case
and no break
statements are needed (or possible).
I know that the general rule is to place a break
statement at the end of each case
statement so that execution doesn't "fall through" to the next case
statement.
When writing iOS apps, I frequently have switch
statements in my -tableView: heightForRowAtIndexPath:
methods. Basically, I often let my cells report the height needed, so I end up with switch
statements like this:
switch (indexPath.row) {
case 0:
return ...
break;
case 1:
return ...
break;
...
default:
return ...
break;
}
I saw this answer, which makes sense to me and is what I expect the answer to be, but that question is specifically about Java, and I wanted to see if the same answer holds true for Objective-C.
I also found this answer, which relates to C, which I assume is the correct answer for Objective-C, as well.
So, is a return
statement a specialized break
statement?
No a return statement is not a specialised break. return causes you to exit the function break causes you to exit the switch statement. You don't need the break if you have the return, but they are different things.
Reading your ideas about switch
and return
, yes, a return
statement can be considered as a specialized break
statement.
A break
is just a branch table abstraction, so as return
is, so at the end of the day, they will simply execute a jump instruction.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With