修复 Xcode 中的代码缩进

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3558709/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 03:32:04  来源:igfitidea点击:

Fix code indentation in Xcode

xcode

提问by Slee

Once I start editing my code and adding for loops or if then statements my code indentation is whacked because the previous code maintains its former indentation instead of adjusting automatically.

一旦我开始编辑我的代码并添加 for 循环或 if then 语句,我的代码缩进就会被重击,因为以前的代码保持其以前的缩进而不是自动调整。

In Visual Studio you can highlight code and select 'format selection' to fix these issues - is there anything similar in Xcode?

在 Visual Studio 中,您可以突出显示代码并选择“格式选择”来解决这些问题 - Xcode 中是否有类似的东西?

回答by Vladimir

Before Xcode 9 (for Xcode 9 see default shortcut below)

在 Xcode 9 之前(对于 Xcode 9,请参见下面的默认快捷方式)

  1. select text
  2. right-click
  3. there's a "Re-indent selection" item in popup menu
  1. 选择文字
  2. 右键点击
  3. 弹出菜单中有一个“重新缩进选择”项


In XCode 4 there is one more step:

在 XCode 4 中,还有一个步骤:

  1. select text
  2. right-click
  3. Click on "Structure"
  4. there's a "Re-indent selection" and other options in popup menu
  1. 选择文字
  2. 右键点击
  3. 点击“结构”
  4. 弹出菜单中有一个“重新缩进选择”和其他选项


Or using the default shortcut:

或者使用默认快捷方式:

  1. select text
  2. press ctrl+ i
  1. 选择文字
  2. ctrl+i

回答by Josh Brown

  1. select text
  2. press Control-I(the default keyboard shortcut)
  1. 选择文字
  2. Control-I(默认键盘快捷键)

回答by Basil Bourque

In Xcode 4.6.3, choose Editor> Structure> Re-Indent.

在 Xcode 4.6.3 中,选择Editor> Structure> Re-Indent

Shortcut: Control + I(that's 'i', for 'indent')

快捷方式:(Control + I即“i”,表示“缩进”)

This works on current selection. If no text selected, choose Edit> Select All(?+A) and then perform the re-indent.

这适用于当前选择。如果未选择文本,请选择Edit> Select All(?+A),然后执行重新缩进。

(I'm more clearly restating the other answers)

(我更清楚地重申了其他答案)

回答by tetrajen

You can do this automatically in Xcode 11

您可以在Xcode 11 中自动执行此操作

回答by David DelMonte

These solutions re-indent text to the default xCode behaviors. To change these behaviors, go to xCode (v.4) > Preferences > Text Editing. Then to Indentation Tab.

这些解决方案将文本重新缩进到默认的 xCode 行为。要更改这些行为,请转到 xCode (v.4) > Preferences > Text Editing。然后到缩进选项卡。

回答by Cons Bulaquena

Xcode 9.2:

Xcode 9.2:

You can select text and press ctrl+ ito re-indent code.

您可以选择文本并按ctrl+i重新缩进代码。

回答by Blago

To quickly re-indent the whole file:

要快速重新缩进整个文件:

?+a

?+a

?+x

?+x

?+v

?+v

Edit: No longer working in Xcode 11

编辑:不再在 Xcode 11 中工作

回答by Jintin

You can try my extension Swimat https://github.com/Jintin/Swimat

你可以试试我的扩展 Swimat https://github.com/Jintin/Swimat

It support format on selection.

它支持选择格式。

Simple install by brew cask install swimat

简单安装 brew cask install swimat