Does the "return ..." statement execute the "break;" command?

I came to Objective-C from the VB.NET background, where the switch Select Case switch , and the break statements are not needed (or possible).

I know that the general rule is to place the break statement at the end of each case so that the execution does not โ€œfailโ€ in the next case .

When writing iOS applications, I often have switch in my -tableView: heightForRowAtIndexPath: methods. Basically, I often let my cells report the required height, so in the end I get the following switch :

 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 from the answer, but this question is about Java, and I wanted to see if the same answer persists for Objective-C.

I also found this answer that relates to C, which I believe is the correct answer for Objective-C.

So, is this a return specialized break statement?

+7
objective-c switch-statement return
source share
2 answers

No return statement is a specialized break. return forces you to exit the function break, forces you to exit the switch statement. You do not need a break if you have a return, but they are different.

+11
source share

There is no magic behind the scene. The only big difference here is that with a break you leave the switch only with a function return. Therefore, the consequences are obvious. If you have a return, you do not need a break, because a break will never be reached. Some IDEs will be grayed out to indicate to you that this is an unreachable piece of code.

+2
source share

All Articles