How to use code formatter in Xcode for Swift?

2020-05-16 15:18发布

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.

9条回答
闹够了就滚
2楼-- · 2020-05-16 15:36

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

查看更多
等我变得足够好
3楼-- · 2020-05-16 15:37

In Xcode [Objective-C, Swift]:

1) +A: highlight code

2) +I: reformat code

查看更多
Summer. ? 凉城
4楼-- · 2020-05-16 15:41

Select lines, then press Ctrl+I.

查看更多
对你真心纯属浪费
5楼-- · 2020-05-16 15:42

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!

查看更多
smile是对你的礼貌
6楼-- · 2020-05-16 15:42

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

查看更多
叼着烟拽天下
7楼-- · 2020-05-16 15:44

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

查看更多
登录 后发表回答