0

So I just recently started developing in Swift and I couldn't seem to find a solution to this online. Brackets in Swift seem to be really counterintuitive and ugly (or maybe I'm just not used to it yet). For example, in Java or C# brackets would look something like this by default in Visual Studio:

if (hello) 
{
    if (yes)
    {
        print("hello")  
    }
}

But in Swift/XCode, it might look something like this.

do {
    let realm = try Realm()
    try realm.write {
        realm.add(self)
    }
} catch let error as NSError {
etc...

Is there a way to change the default bracketing system such that every time I open up a bracket, it doesn't look so unformatted?

Brian Park
  • 1,245
  • 3
  • 10
  • 15
  • 1
    FYI - the Swift code you posted is perfectly formatted. It's actually a very common style. It's perfectly valid in Java and C# as well. Any language using `{ }` allows them to be used in several different style. The style is a user preference, not a language style. What's odd about your Java/C# example is the inconsistent style of the braces. On the first `if` the braces are not indented (common) but the second `if` they are indented (uncommon). It's more important to be consistent with one style and stick with it. Avoid mixing styles in your own code. – rmaddy Jul 03 '18 at 04:13
  • Oh whoops, thanks for catching that! It was a simple mistake on my part. – Brian Park Jul 03 '18 at 04:21
  • `really counterintuitive and ugly (or maybe I'm just not used to it yet)` That's what I think about your style :) (which is convention for C#, but not Java). I mainly dislike it because it wastes so much vertical space. All of our monitors are widescreen, we have plenty of horizontal space to spare. I'll take any opportunity to spend horizontal space to save vertical – Alexander Jul 03 '18 at 04:33
  • Also, Google's Swift style guide recommends: "There is no line break before the opening brace ({), unless required by application of the rules in Line-Wrapping." https://google.github.io/swift/#braces – Dan Karbayev Jul 03 '18 at 08:21

0 Answers0