10

I have noticed that the coding style in Swift appears to be to put braces on the same line as a method signature or if statement etc. Without getting into a debate about which is right or wrong, my aim is simply tom get Xcode to put the braces on a new line when it does its auto completion and for the new file templates.

I've tried the advice here and it doesn't work: https://forums.developer.apple.com/thread/23087

I'm using Xcode 8.1 on Sierra.

Has anyone else been able to get this to work?

Thanks, Alan

Alan Spark
  • 8,152
  • 8
  • 56
  • 91
  • Old, but may help http://matthew.delmarters.com/weblog/getting-xcode-to-put-opening-braces-on-a-new-line/ – Code Different Dec 10 '16 at 15:20
  • Thanks, I'd seen that but unfortunately it has not changed the behaviour of the templates or auto complete functions. Maybe I need to find the location of the templates and edit them myself. – Alan Spark Dec 10 '16 at 16:16

1 Answers1

2

You can use below methods in conjunction to accomplish this task.

ClangFormat and Uncrustify

Above method will only change the default statement templates and not the inbuilt methods formatting. (i.e autofilled viewDidLoad() method will still have the braces in the same line.) For this you should consider using these plugins. They have options to save the file with specified format.

(UPDATE Snippet Edit no longer works)

Snippet Edit

Snippet Edit is a small program that is used to edit the Xcode's standard code snippets. We have to do this way because there is no direct way to change the code style in Xcode settings and XCCodeSenseFormattingOptions is not supported since Xcode 4

You can follow below screenshot for reference. Remember to restart Xcode once you are done with editing.

enter image description here

Penkey Suresh
  • 5,816
  • 3
  • 36
  • 55
  • 1
    That's great, thanks. I've been through all of the snippets and changed them to my liking and it works perfectly. The next thing I'll be looking at is the file templates that are used when you create a new swift class. These must be stored somewhere too. I don't think they fall under the snippet category. – Alan Spark Dec 11 '16 at 09:56
  • @AlanSpark glad to know. Coming to the classes and their inbuilt methods (example viewDidLoad() of UIViewController) there were plugins which convert already written code to above said format. But my guess is it would be really hard to change them while autofill suggestion itself. – Penkey Suresh Dec 11 '16 at 10:05
  • @AlanSpark for the libraries you can refer to this question http://stackoverflow.com/questions/25942904/default-opening-curly-braces-in-xcode – Penkey Suresh Dec 11 '16 at 10:11
  • Thanks for that link, I'll take a look at some of the ideas there. – Alan Spark Dec 11 '16 at 16:56
  • Neither ClangFormat nor Uncrustify operate on Swift code. – jscs Oct 23 '17 at 12:49
  • 2
    Snippet Edit's been discontinued. – Hanii Puppy Nov 06 '19 at 11:56