How to use code formatter in Xcode for Swift?

XcodeSwiftCode Formatting

Xcode Problem Overview


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.

Xcode Solutions


Solution 1 - Xcode

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

Solution 2 - Xcode

I write a extension app about format Swift code: Swimat

There has two way to install

  1. brew cask install swimat

  2. Clone this repo, build and restart Xcode.

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

preview

Solution 3 - Xcode

+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.

Solution 4 - Xcode

In Xcode [Objective-C, Swift]:

1) +A: highlight code

2) +I: reformat code

Solution 5 - Xcode

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!

Solution 6 - Xcode

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.

Solution 7 - Xcode

(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.

Solution 8 - Xcode

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

Solution 9 - Xcode

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

Solution 10 - Xcode

Select lines, then press Ctrl+I.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionS. BirklinView Question on Stackoverflow
Solution 1 - XcodeCuriousRabbitView Answer on Stackoverflow
Solution 2 - XcodeJintinView Answer on Stackoverflow
Solution 3 - XcodeAdd080bbAView Answer on Stackoverflow
Solution 4 - XcodeHamid YusifliView Answer on Stackoverflow
Solution 5 - XcodeAntonio CiolinoView Answer on Stackoverflow
Solution 6 - XcodeaskielboeView Answer on Stackoverflow
Solution 7 - XcodeToerndevView Answer on Stackoverflow
Solution 8 - XcodeashchkView Answer on Stackoverflow
Solution 9 - Xcodeuser2961953View Answer on Stackoverflow
Solution 10 - XcodeMohamed Abdul-RaoufView Answer on Stackoverflow