Xcode 中的代码格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1510915/
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
Code Formatting In Xcode
提问by itsaboutcode
Is there anyway to format the code in Xcode like you can do in Eclipse or Netbenas. I have not found any option in menu, is there any hot key for this or its simply not in Xcode?
无论如何,是否可以像在 Eclipse 或 Netbenas 中那样在 Xcode 中格式化代码。我在菜单中没有找到任何选项,是否有任何热键或它根本不在 Xcode 中?
采纳答案by nall
Select some text and then: Edit->Format->Re-Indent
选择一些文本,然后:编辑->格式->重新缩进
You can bind this to a hotkey in the preferences.
您可以将其绑定到首选项中的热键。
回答by Dylan Markow
In Xcode 4, it's been moved to Editor > Structure > Re-Indent
command (and has a default shortcut of CTRL+I).
在 Xcode 4 中,它已被移至Editor > Structure > Re-Indent
command (并具有默认快捷方式CTRL+ I)。
回答by Dave DeLong
Other than re-indentation (Edit > Format > Re-Indent), not really. However, Xcode does have support for scripts (the menu to the right of the Window menu), so you could conceivably write a script that formats your code how you like it.
除了重新缩进(编辑> 格式> 重新缩进),不是真的。但是,Xcode 确实支持脚本(Window 菜单右侧的菜单),因此您可以想象编写一个脚本,按照您喜欢的方式格式化代码。
Edit: here are some links that touch on this subject:
编辑:这里有一些涉及这个主题的链接:
回答by swiftBoy
Here are the shortcuts, to format the code in Xcode
这是在 Xcode 中格式化代码的快捷方式
1.Format entire code (entire class/controller)
1.格式化整个代码(整个类/控制器)
select the entire code and press ?+|on macto format your code.
选择整个代码并在 Mac上按?+|来格式化您的代码。
2.Format particular block of code
2.格式化特定的代码块
select the code and press
选择代码并按下
?+]for right move and ?+[for left move
?+]为右移,?+[为左移
Note : as per @JavierGiovannini sugesstion you can do using Editor Menu option
注意:根据@JavierGiovannini sugesstion,您可以使用编辑器菜单选项
3.Select code --> Go to Editor --> Structure --> Re-Indent
3.选择代码 --> 转到编辑器 --> 结构 --> 重新缩进
回答by geowar
My personal favorite code formatter is Uncrustify. It has many, many options, so I also suggest you download UniversalIndentGUI, a GUI to help set Uncrustify's behavior to your liking.
我个人最喜欢的代码格式化程序是Uncrustify。它有很多很多选项,所以我还建议您下载UniversalIndentGUI,这是一个 GUI,可以帮助您根据自己的喜好设置 Uncrustify 的行为。
回答by Shaheen Ghiassy
Not saying this is best approach, but for completeness, if you cut and then paste the code back in, Xcode will automatically format it for you.
并不是说这是最好的方法,但为了完整起见,如果您剪切并粘贴代码,Xcode 会自动为您设置格式。
回答by Guy Daher
XCode 8 Extensions
XCode 8 扩展
Another option is to use an extension like Swimat. Does the job for me. However, the caveat is that it does not format on save due to the restrictive Xcode Extension APIs, but they're trying to find a workaround.
另一种选择是使用像Swimat这样的扩展程序。为我做这份工作。但是,需要注意的是,由于 Xcode 扩展 API 的限制,它不会在保存时格式化,但他们正在尝试寻找解决方法。
回答by Paresh Mangukiya
If you love looking your code like me, you really need this shortcut :)
如果你喜欢像我一样看你的代码,你真的需要这个快捷方式:)
You have to select with CMD + Afor reformatting whole code segment.
您必须使用CMD + A选择重新格式化整个代码段。
All you need to do this, select the code segment which you want to reformat it and press : Ctrl + IMagically, It is just reformatted
所有你需要这样做,选择你想要重新格式化的代码段,然后按: Ctrl + I神奇地,它只是重新格式化
回答by themefield
^-I(control- capital i) : format the current line
^- I( control- capital i) : 格式化当前行
?-A+ ^-I: format the whole code file
?- A+ ^- I: 格式化整个代码文件
回答by jeff
You can also highlight the code you want to re indent, and use the re-indent command
也可以高亮要重新缩进的代码,并使用re-indent命令