问题
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?
回答1:
Select some text and then: Edit->Format->Re-Indent
You can bind this to a hotkey in the preferences.
回答2:
In Xcode 4, it's been moved to Editor > Structure > Re-Indent
command (and has a default shortcut of CTRL+I ).
回答3:
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.
Edit: here are some links that touch on this subject:
http://hackertoys.com/2008/09/18/adding-a-code-beautifier-script-to-xcode(dead link)http://8020world.com/jcmendez/2006/11/geeky-stuff/software/adding-a-script-menu-to-xcode-to-reformat-code-using-astyle(dead link)
回答4:
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.
回答5:
Here are the shortcuts, to format the code in Xcode
1.Format entire code (entire class/controller)
select the entire code and press ⌘+| on mac to format your code.
2.Format particular block of code
select the code and press
⌘+] for right move and ⌘+[ for left move
Note : as per @JavierGiovannini sugesstion you can do using Editor Menu option
3.Select code --> Go to Editor --> Structure --> Re-Indent
回答6:
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.
回答7:
In v.4 you can make some adjustments through xCode preferences...
Fix code indentation in Xcode
回答8:
You can also highlight the code you want to re indent, and use the re-indent command
回答9:
XCode 8 Extensions
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.
回答10:
Try this solution: ⌘A [⌘K ⌘F]
来源:https://stackoverflow.com/questions/1510915/code-formatting-in-xcode