Why is the "case" inside the switch expression negative indented?

I don’t think this is completely the Swift / Xcode thing, since I saw it in other languages ​​and in the IDE.

Why is the β€œcase” inside the switch statement negative indented (I'm not sure if this is the correct way to formulate)?

I would expect the Switch statement to look something like this.

switch(type) { case 1: // do something break; case 2: // do something else break; default: // default break; } 

But Xcode insists on it

 switch(type) { case 1: // do something break; case 2: // do something else break; default: // default break; } 

Is this a mistake, or is there a reason for this? If so, then what is it? This is what has been listening to me for quite some time.

+6
source share
1 answer

Well, I would suggest that the break statement refers to the "section" in the case clause. And like any other statement, it is indented with respect to case . As for case regarding switch - well, I don't know.

But I'm completely with you - and formatting is a matter of personal preference. Since formatting rules are not explicitly defined in Xcode, this cannot be an error;)

FWIW, I prefer this style

 switch x { case 1: // do something break case 2: // do something else break default: // default break } 
+6
source

All Articles