Pages

Categories

Archives

XCode: The Starting Curly Brace

As I dive deeper into the world of programming for Apple hardware I bring along with me past practices or habits that I must change in order to fit into the Objective-C world. It has been years since I last touched C/C++ and coming from a world of .NET and C#, my first impressions of XCode and Objective-C was basically taking a few steps backwards.

I couldn’t have been more wrong…

I see the power of what this new world has to offer and thus as I continue my journey I am finding myself taking new approaches to situations and even finding that some old habits I just flat out prefer to hold on to. Today, I’m talking about the placement of the starting curly brace.

Now, it seems to be common place in the C/C++ world where the starting curly brace is on the same line as the starting block of code. For example…

1
2
3
void someFunction() {
  //code...
}

In the C# world it seems to be common place to place the curly brace on a new line. For example…

1
2
3
4
public void someMethod()
{
  //code...
}

There are various arguments on why each one is better. Commonly I hear that the first method is better because it doesn’t have wasted space which results in less scrolling. While on the flip side, I commonly hear that by placing the curly brace on a newline it makes for easier readability. In the end, it really only comes down to personal preference as the placement has zero effect on the result of the executed code.

Personally, I prefer and have always preferred placing the curly brace on a new line. Why is this? Well, it is what I got used to while using Visual Studio for so long. It has occurred to me that the big named IDE’s out there seem to have a bit of a role, or rather influence, in how the community adopts the typical way a programming language is written out. With the exception, of course, of those individuals who take the time to rummage through the IDE’s preferences and change auto features to their liking.

Which is where my recent adventure comes into the story. While working in XCode I noticed the predicting text feature (amazing by the way, really wish Visual Studio had this) would always place the starting curly brace on the same line as my starting block of code. After a short period I stopped relying on the predicting text feature and just wrote everything out so that I would have to interrupt my typing flow to move the curly brace.

The image above shows XCode’s auto predicting magic (seriously, I have no clue what it is actually called) on an ‘if’ statement. Getting a little tired of having the starting curly brace on the same line I went through XCode’s preferences to change it but was given a little shock. There are no options to change this from the IDE.

I was a little disappointed but had not given up just yet as I did recall that the Apple world revolves around plist configuration files and held hope that maybe there was still a way.

Home: Library->Preferences->com.apple.Xcode.plist

I had found the plist file in the above location and as I opened it and looked through I couldn’t find any settings that would allow me to make the change I wanted. Looking around online I found what I was looking for, I needed the following inside this plist:

XCCodeSenseFormattingOptions as a dictionary along with BlockSeparator inside the newly created dictionary with the string value of a newline character.

I tried doing this manually through the plist editor that comes with the OS but I couldn’t get it to work correctly and so looking around online I found out how to manually edit plists from the terminal:

defaults write com.apple.Xcode XCCodeSenseFormattingOptions -dict BlockSeparator “\\n”

By typing in the above line I had achieved my goal:

Now when I type such statements as ‘if’ this is what automatically shows up in XCode.

The issues I had doing it manually through a plist editor was that I needed that hidden newline character ‘\n’ but typing it out manually only cause actual characters to be printed: For example…

if (condition)\n{

statements

}

So, with that if any of you, like me, prefer to have their curly braces start on a newline in XCode. Just the bolded line above in a terminal and you should be all set. Happy Coding!

NOTE: If you copy and paste the bolded line be careful of the quotes. Might just want to type that part in by hand. It has been my past experiences that showed copying lines of code with single and double quotes from a website does not always go over too well everytime.

15 comments to XCode: The Starting Curly Brace

Leave a Reply

Connect with Facebook

  

  

  

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">