Author Topic: What's up with Xcode's screwy Switch statement indentation?  (Read 10438 times)

sPecan

  • Newbie
  • *
  • Posts: 5
    • View Profile
Every time I need to enter a switch statement for some example in the book, XCode indents it awkwardly. I'm using XCode 6.4

For example, in page 441, Chapter 13, Creating Custom Cells, Step 15.

If I type Switch, then press enter to auto complete the template, I get this:

Code: [Select]
switch (<#expression#>) {
  case <#constant#>:
<#statements#>
break;

  default:
break;

Which is strange, because the template looked right. I can't copy/paste the template because it disappears every time I try to.

The template continues to be a nuisance throughout. Because even if I fix the indentation, when I enter new lines, it insists on messing it up again. For example, my XCode is set to indent with Tabs equivalent to 4 Spaces. But when I enter a new statement under a Case label, it moves the indentation in to 8 Spaces (when fixing the indentation, I'm aligning the Case label with the Switch statement).

It looks like a bug, but I can't find any information through Google about this bug (other people are complaining about Switch indentation, but they're having different problems). So, maybe it isn't a bug. If it isn't, I'd like to understand what's going on here.


kjmcneish

  • Administrator
  • *****
  • Posts: 719
    • View Profile
Re: What's up with Xcode's screwy Switch statement indentation?
« Reply #1 on: July 26, 2015, 03:51:21 AM »
The Objective-C Switch statement code snippet should look like this:

Code: [Select]
switch (<#expression#>) {
        case <#constant#>:
            <#statements#>
            break;
           
        default:
            break;
    }

You're getting the strange indenting because the compiler doesn't understand the tokens between # signs. i'm not sure what's going on here, but is it possible you have created your own switch code snippet?

All the best!
Kevin

sPecan

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: What's up with Xcode's screwy Switch statement indentation?
« Reply #2 on: July 26, 2015, 09:19:43 AM »
I managed to get the popover out of the way of the template by trying to make a switch statement at the bottom of the file. Here's what it looks like.

http://imgur.com/mgTW81g

I haven't created any custom snippets. I searched for the switch snippet, but the template in there is correct!

Any idea how I can fix this?

---

Actually, on closer inspection, the Switch snippet is wrong! Here it is.

http://imgur.com/PPj5nts

It's using a 2-space indent. But I can't edit it because it's the default code snippet.
« Last Edit: July 26, 2015, 09:28:25 AM by sPecan »

kjmcneish

  • Administrator
  • *****
  • Posts: 719
    • View Profile
Re: What's up with Xcode's screwy Switch statement indentation?
« Reply #3 on: July 27, 2015, 05:50:07 PM »
You may need to uninstall and reinstall Xcode.

All the best!
Kevin

sPecan

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: What's up with Xcode's screwy Switch statement indentation?
« Reply #4 on: July 28, 2015, 11:41:04 PM »
I tried reinstalling XCode, but that didn't work. Thinking the problem was just with me (because you don't seem to have this problem), I asked on stackoverflow.com. I didn't get an answer, but I did get a hint. So I tested indenting with Spaces instead of Tabs. The indentation works correctly if I use Spaces! But I don't like spaces; I like tabs. Regardless, I'm curious if it's just me.

I have this indentation problem when I indent using Tabs, the Widths is 4, and the Indent is 4. Interestingly, the outdented Case labels are indented exactly 2 spaces, which is what appears to be wrong with the snippet.

Btw, this only happens with Objective-C files. In Swift files, the snippet doesn't autocomplete at all.

Also, I wonder why the indentation would work correctly with Spaces. I mean, the Case labels are indented only 2 spaces in the snippet, so why do they suddenly indent 4 spaces when I indent using Spaces?