6

I don't think this is entirely a Swift / Xcode thing, as I've seen it in other languages / IDEs as well.

Why is 'case' inside a switch statement negative indented (I'm not sure if that's the correct way of wording that)?

I would expect a 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 this

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

Is this a bug, or is there a reason for this? If so, what is it? It's something that has bugged me for quite some time.

Luke Berry
  • 1,927
  • 4
  • 19
  • 32

1 Answers1

6

Well, I would guess that the break statement belongs to the "section" in the case clause. And as any other statement, it is indented relative to the case. As for the case relative to switch - well I don't know.

But I'm completely with you - and formatting is a matter of personal preference anyway. Since the formatting rules in Xcode are not explicitly defined - it cannot be a bug ;)

FWIW, I prefere this style

switch x {
    case 1:
        // do something
        break
    case 2:
        // do something else
        break
    default:
        // default
        break
}
CouchDeveloper
  • 18,174
  • 3
  • 45
  • 67
  • Yeah, I also prefer this style but Xcode has a big problem - it is years behind in functionality compared to other IDEs. – Sulthan Oct 13 '15 at 17:44
  • 3
    By the way, in Swift, you don't need any `break` in a `switch`. It's implicit. You have the `fallthrough` statement if you want to force a fall through cases – BPCorp Oct 13 '15 at 20:01
  • @BPCorp You are correct. But a case clause should also have at least _one_ statement in Swift. – CouchDeveloper Oct 14 '15 at 07:45