Does Xcode have code autoformat feature?

IphoneXcode

Iphone Problem Overview


Is any way to autoformat a source code in the Xcode IDE?

Iphone Solutions


Solution 1 - Iphone

There isn't really an autoformat option in Xcode.

There is an option ^I (Control-I) to re-indent the code, which will re-align the code according to the tab width set in your preferences, but that's about as far as it goes.

You might have better luck with a text editor like TextMate? I don't know for certain, but I know it supports Objective-C and has some macros, there may be a reformat option?

Solution 2 - Iphone

Yes Xcode 4 has an autoformat feature.

Try following:

  1. Select text. CMD+A.
  2. Press CTRL+I .

Please also see here: https://stackoverflow.com/questions/3558709/fix-code-indentation-in-xcode

Solution 3 - Iphone

One trick is to select the code (either "select all" or just the section you want to re-format) - then cut, then paste it back. This triggers the automatic code formatting in the Xcode editor.

Solution 4 - Iphone

ctrl + I will auto format the code you're selecting

Solution 5 - Iphone

ClangFormat-Xcode is an Xcode plugin available in Alcatraz. This is a nice code format tool.

Solution 6 - Iphone

I found that the Copy & Paste method (Cmd-A then Cmd-C then Cmd-V) worked in XCode 4 and was the method I used whilst I was waiting for google to display the results (Mac was going slow!)

Solution 7 - Iphone

To change, go to xCode => Preferences => Text Editing => Indentation => Automatically indent based on syntax.

enter link description here

Solution 8 - Iphone

Format on Save

For anyone wanting to format on save this is what I did. It's a bit janky but it works well. ⤵


  1. To get started pick out a formatter for Xcode. I'd recommend SwiftFormat but here's a list of a bunch of them and other handy extensions.
  2. Once you have a formatter picked out assign it a hotkey in Xcode that's not taken such as CMD + ~
  3. Then download a shortcut mapper such as BetterTouchTool.
  4. In your shortcut mapper simply setup CMD + S for Xcode and set it to actually use your shortcut you set for your fomatter. Like this ⤵ enter image description here

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
QuestionsashaeveView Question on Stackoverflow
Solution 1 - IphoneJasarienView Answer on Stackoverflow
Solution 2 - IphoneArtem KalachevView Answer on Stackoverflow
Solution 3 - IphonePaul RView Answer on Stackoverflow
Solution 4 - IphoneYiyue WangView Answer on Stackoverflow
Solution 5 - IphonechancyWuView Answer on Stackoverflow
Solution 6 - IphonenetniVView Answer on Stackoverflow
Solution 7 - IphoneIndraView Answer on Stackoverflow
Solution 8 - IphonetyirvineView Answer on Stackoverflow