How to use code formatter in Xcode for Swift?
Asked Answered
N

10

92

Xcode code formatter Swift

I'm trying to auto format my code written in Swift. It's necessary if you're working in team and using CVS and don't want to merge for hours.

Ctrl+I just makes an indent, but for example i want to have proper space between functions and just all the stuff like in Clang.

Noblewoman answered 18/11, 2014 at 12:7 Comment(1)
Does this answer your question? Code Formatting In XcodeDillon
W
90

The reformat code command in xcode is Ctrl-I, not Cmd-I and it appears to work just fine.

Wonderful answered 23/11, 2014 at 3:44 Comment(8)
Yeah, that's what i meant. Ctrl-I.Noblewoman
Control + I seems to perform indentation on Swift and Objective C, but I would like to see a more complete syntactical prettification ala Visual Studio and Resharper in Xcode - but with that being said, I think Xcode is on its way to becoming a usable IDE.Forthwith
Ctrl+I seems to have no effect on a text selection for me in XCode 7 beta 5Eightieth
If you are using XCode, you can try my plugin: Swimat - github.com/Jintin/SwimatEuclid
control-I does not reformat the code, it merely "balances" each line. so, it does not enforce all the necessary newlines, indentation and so on that is part of the standard idiom for control structures in Swift. (confusingly, Xcode has the ability to automatically format your Swift as you type, but there's no button to "fix" batches of it later!)Ferriter
Xcode is still no where close to VSCode, they just fixed multi tabbing in Xcode 12. May be by Xcode 100 Pro Max Plus we will have format on save.Fergus
It's an upper i, not a lower L.Geanticlinal
Yea this is blatantly an incorrect answer; ^I doesn't format beyond indentation at all.Gravelblind
E
65

I write a extension app about format Swift code: Swimat

There has two way to install

  1. brew install swimat --cask

  2. Clone this repo, build and restart Xcode.

See https://github.com/Jintin/Swimat for more information

preview

Euclid answered 23/11, 2015 at 14:37 Comment(4)
I think you should mention or illustrate if Swimat handles formatting spaces between lines. That information is not illustrated or communicated either here or on the github page. As per the gif illustration, Xcode default formating handles that already.Defoe
Swimat will do as default Xcode do, which is remove empty line space. But not remove empty line. Thank you for your feedback, we'll update the description later.Euclid
Nice, I wish there is one for Objective-C too.Messmate
dude, you did awesome job. why no one else is pointing to this solution? there are several open question on formatting in xcode.Dillon
A
33

In Xcode [Objective-C, Swift]:

1) +A: highlight code

2) +I: reformat code

Antilepton answered 3/10, 2019 at 19:43 Comment(4)
this should be the answer even for in 2021. You have to select the code (cmd + A) first then ctrl + I will format it for you.Cally
⌃+I: only reindents, doesn't reformat. At least for swift anyway. Try for example to reformat: struct Tag {var name: String } It won't do anythingKalk
What's ? That's just a character above my 6 isn't it? 😅Saltation
No, it's control on Mac. BTW, Xcode does not work on windows or linux.Dictionary
U
22

+i: reformat code
Ctrl+i: fix code indentation

Eventhough it has good IntelliSense, xcode sucks at code formatting generally.
Wish it was better.
Apple has to spend so much effort in this in order to make it VS level good.

Unhandy answered 26/11, 2014 at 16:15 Comment(3)
Control + i does work with Swift. In what way do you see code formatting working better with Objective C as then Swift? Just curious.Forthwith
Control + i only indents code in swift. It does not fix spaces after brackets etc.Maiolica
<kbd>^</kbd> just means <kbd>ctrl</kbd>--why are these listed as separate shortcuts? Both are the exact same key combinations. <kbd>^i</kbd> re-indents--it does not reformat.Frustration
D
15

SwiftFormat is a popular code formatter for swift. It has an extension for Xcode, which can be accessed from the Editor menu.

Quote from the SwiftFormat readme:

SwiftFormat is a code library and command-line tool for reformatting swift code on macOS or Linux.

SwiftFormat goes above and beyond what you might expect from a code formatter. In addition to adjusting white space it can insert or remove implicit self, remove redundant parentheses, and correct many other deviations from the standard Swift idioms.

Daub answered 25/2, 2019 at 12:28 Comment(1)
Any clue why this isn't built into xcode? this would be far more useful than the mostly-useless indent functionality they have built-in.Gravelblind
L
14

Xcode and Swift 2.0: Per file, Cmd-A then Ctl-I. Ctl-I works for what is highlighted, Cmd-A highlights all. Just be careful not to accidentally delete all your source after the Ctl-A!

Leathaleather answered 26/1, 2016 at 14:19 Comment(0)
H
1

(I know the question says Xcode but) personally this is another reason to use AppCode alongside Xcode.

While Xcode only fixes the indentation for Swift code, as mentioned in the question, AppCode at least handles spaces too. Somewhat relatedly it removes indents from empty lines by default.

AppCode's Swift support definitely isn't perfect yet and you still need to check the code in both, but it's still worth it because of better navigation and things like this.

Harass answered 14/2, 2018 at 14:52 Comment(0)
T
0

The command in xcode is ctrl+i to format code, not cmd+i.

Townsfolk answered 10/3, 2022 at 19:9 Comment(0)
H
-4

I would suggest using sublime text with the swift plugin, which will allow you to hotkey the code formatting. https://sublime.wbond.net/packages/Swift

Hyperbolize answered 22/11, 2014 at 23:15 Comment(5)
I don't think this is very efficient - you're saying OP would have to open each file (individually) in a separate program just to format them?Edvard
The suggestion was not to re-open them. Developers at my job work in sublime text, not xcode.Hyperbolize
To be fair though, OP was asking about how to auto-format his code in Xcode. So to suggest that he do so in Sublime Text is either saying (1) he open them separately, or (2) he switch editors.Edvard
I am not so much concerned about "being fair" (fair to who?) as offering a solution. Sometimes it is necessary to think outside the box. This solution might fall into that category. Thanks for your opinion though.Hyperbolize
I just install swift plugin for Sublime, but seem like Sublime doesn't have hotkey or menu to make the format.Craze
E
-6

Select lines, then press Ctrl+I.

Egypt answered 19/10, 2019 at 14:34 Comment(1)
When answering a four year old question with eight other answers please take the time to make sure your answer isn't a duplicate, and if it isn't to point out what new aspect of the question your answer addresses. Also formatting for keystroke combinations can be done with the use of <kbd> and </kbd> around each key.Assisi

© 2022 - 2024 — McMap. All rights reserved.