Categories

Archives

Objective-C Tip: Switch Statements

During one of my sessions of working on the iPhone game. I noticed something very strange within Objective-C.

1
2
3
4
5
6
7
8
switch(someVar)
{
  case 1:
    int x = 5; // Expected expression before 'int'
 
    break;
  ...
}

Apparently, this will get a compiler error as it expects an expression to be place before the declaration. I did a little searching around and was unable to find much on why this causes an error. I did however, find a few solutions that seem to work really well.

Considering the error is that the compiler expected an expression before the declaration you can try to rearrange your statements so that declarations are not the first statements within the case statement.

1
2
3
4
5
6
7
8
9
switch(someVar)
{
  case 1:
    [self someMethodCalled];
    int x = 5; // Compiles fine
 
    break;
  ...
}

…or you can just place an empty semicolon if the above example is not an option.

1
2
3
4
5
6
7
8
9
switch(someVar)
{
  case 1:
    ;
    int x = 5; // Compiles fine
 
    break;
  ...
}

Last, I found a better solution to the above examples and it is the one I’ll be using. I really don’t know exactly why this one works as it is not really an expression but by adding { and } to separate the block of code within the case statement. We are allowed to place our variable declarations at the top.

1
2
3
4
5
6
7
8
9
switch(someVar)
{
  case 1:
    {
      int x = 5; // Compiles fine
    }
    break;
  ...
}

I like this solution the best cause it continues to give me the freedom on where I want to place my statements and expressions and, as a double whammy, helps in readability since the block of code will now be indented one block.

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.

iTunes Error 7: Missing CoreGraphics.dll

So, I get home from work today only to find that iTunes on my windows machine would not start up. I kept getting the message that CoreGraphics.dll was missing and that I should re-install iTunes. I really don’t know HOW this could have happened as I have not installed/uninstalled anything on here.

I searched around online for awhile and saw the typical suggestions of uninstalling and reinstalling and so I did just that. I uninstalled and reinstalled iTunes and the issue existed. So I did a full blown search in my C:\ and sure enough CoreGraphics.dll was sitting in “C:\Program Files(x86)\Common Files\Apple\Apple Application Support\”. No where else was it found.

I then decided to poke around inside the iTunes64Setup.exe install file. Luckily, WinRAR was able to extract the setup which presented me with several Apple install files.

  • AppleApplicationSupport.msi
  • AppleMobileDeviceSupport64.msi
  • AppleSoftwareUpdate.msi
  • Bonjour64.msi
  • iTunes64.msi
  • MobileMe.msi
  • QuickTime.msi
  • SetupAdmin.exe

Using msiexec I was able to extract the files within the msi install files (all except for the SetupAdmin.exe of course):

msiexec /a FileName.msi /qb TARGETDIR=PathToWhereYouWantTheFilesExtracted

Since I had everything extracted in their own folders, from there I did a full blown search to see where all the CoreGraphics.dll’s were located. I was under the hunch that we were NOT going to find CoreGraphics.dll within the iTunes install itself considering where I found it on my system. Sure enough, I only found 1 instance of CoreGraphics.dll and it was within the install of AppleApplicationSupport.msi.

I have been unable to figure out WHY iTunes would be shipped without certain key dll’s when it, apparently, won’t run without them. However, by reinstalling AppleApplicationSupport.msi I was able to get iTunes working again.

While I’m not 100% this will fix everyone’s issue with a missing CoreGraphics.dll, I would at least suggest trying to uninstall NOT iTunes but AppleApplicationSupport and reinstalling that install directly. Hope this helps some people.

I’m Back!

So I’ve been neglecting this site for awhile now. For those of you who read this blog, I apologize for being so quiet. I ended up taking up 2 jobs which took up a lot more of my time than I had realized and I didn’t have much time to work on my game much less make any new posts here.

So what have I been up to? Well, since my last post I’ve broken down and bought a MacBook. For various reasons but most importantly to finally dive in the iPhone SDK. A lot of my attention has been spent playing with Objective-C, XCode, and the iPhone SDK and I have to admit that it is quite enjoyable to mess with. It is taking me some time to get used to as Objective-C is just so strange to me but nothing I can’t handle.

I do hope that if I become familiar with the language and API that I may have something up on Apple’s AppStore but we all know how my home projects have been. I do hope this can finally keep my interest. Here are a few screenshots of what I’ve been doing.


They are not much but the first shows a simple menu screen which goes into the game screen shown by the other 2 images. The last image shows that the squares are clickable. I really don’t know where I’ll be going with this but we’ll see what comes up as I continue learn the environment.

On a side note, I am loving my new MacBook and the Mac OS X. This is my first time since having an Apple II in my middle school where I’ve touched a mac. I had no idea just how easy it is to use, everything just works and works really well.

Out of Town

I’ll be leaving for a week, starting today, for a work related trip. Nothing much to say about it but I won’t be able to work on my game any until I get back due to the lack of owning a laptop. I really need to buy a laptop one of these days. Anyways, hope everyone’s week goes goes smoothly.