Rethrow An Exception in C#

So generally, when I want to re-throw an exception that I caught in C#, I just throw a new exception like this:

This just never sat well with me, so today I decided to figure out if there was a better way to handle this.  Seems like there just had to be.

Shoot, it is so simple!

throw (ex); will re-throw the exception that was caught as if it was not caught.

If by chance you didn’t catch the exception object, you can still re-throw it like this:

I am in shock that all these years I was unaware of this.  Enjoy!  And thanks to the Microsoft docs, which I should visit more often.

How To Auto-Increment Your iOS Version Number

Previously I wrote up how to auto-increment your build number.  I mentioned how I wanted to figure out how to do the same for the version number.  The thing that makes the version number different and more difficult is that version numbers generally have sub-versions.  My bash scripting skills are minimal so I did not take on the task at the time and planned to revisit it.

Well Paul Capestany picked it up where I left off.  You can see his solution on github.  To understand how to install and use this script, please see the previous post for instructions.

I like Paul’s solution, but one thing that does not appeal to me is that the build number is used for the last piece of the version number, i.e. 2.4.X.  The way I see things, the build number is a continuously incrementing number that should never be reset.  So I went on and edited the script so that the last section increment by one every time, just like the build number.  The intention behind this is that it will be reset (manually of course) when you do major version updates.

Here is the script that I ended up with (you can find the github gist here):

I ended up leaving the incrementing of the build numbers in there, as I generally use both and I figure pretty much everybody else will as well.

One thing that may come up is that this only works for the last section of the version number.  I figure you always want to update this piece.  Unfortunately you cannot do the same with the first two sections.  The computer really cannot tell when you are releasing a “significant” build, so as mentioned previously it is best to update those two manually when you make major revisions to your app.

Thanks again to Paul Capestany for commenting on the previous post with his solution.  It was immensely helpful in coming up with this.

My objective-c return type is giving “Expected a type” error

Man, I hate when the solutions are so simple.  Well for me, I had added the type into the implementation of a class, it was working fine.  Then I needed to add it as the return type.  Check.  Update the interface so that callers can see it.  Check.  But then I was getting an error: Expected a type.  Seemed weird as everything was working fine the implementation file.

In my defense I didn’t write this class, I inherited it.  I didn’t realize that there were some imports in the implementation file that were not in the header file.

SOLUTION: Move the import from the implementation to the header file.

Make sure that you have the correct import.  It’s one of those little bugs/mistakes that make you shake your head… at yourself.

FYI, a couple of other possible issues/solutions mentioned on this stackoverflow are circular imports or a typo in the previous line.

Stop Forced Portrait Orientation in iOS App

I have an iOS app at the office that I have inherited, and one of the bugs that I found when I started was this:

I have the device in a landscape orientation.  I have a UINavigationController with a UITableView (we will term as the “parent”); pushed another UITableView (we will call him the “child”); then pop the child off with the nav back button; and when the view returns to the parent the device orientation always changes to portrait.  Event if I try rotating the device at this point, it still stays in a portrait orientation.

I have several similar scenarios in the app, but there was only one exhibiting this problem.  Well even though I could not find anywhere in the code that forced a portrait orientation, after searching the web for a little while to find the solution I decide to just try and be a little more explicit with things.  So I add this code into the parent view.

Guess what.  It works!  I am still not sure what the cause was before.  I did a quick search through the Apple docs to see what the default is for this method (if there is a super), but I did not see anything about defaults.

I am still new to iOS development, but I am learning not to take anything for granted.  Before wasting time search for solutions to bugs, make sure that your code is explicit for the situation.  Being explicit also helps make things easier to debug as you can trace though those methods.

How To Auto-Increment Your iOS Build Number

UPDATE: The scripts on this page have been updated to auto-update the version number as well and can be found here.

Things do not always go smoothly in Xcode, as I have mentioned before.  Since I have had to do many builds with small tweaks and upload them to TestFlight to test my app, I started to get lazy with my build numbers.  As such, I decided that it was time to figure out how to automate incrementation of it.

There is a lot on the web documenting different ways to this.  Many of them seem overly complicated.  The two most helpful sources for me were by Fredrik Olsson and, of course, StackOverflow.  With these posts and their comments I was able to work up a solution that met my requirements:

  1. Be as simple as possible.
  2. Only increment on release and ad hoc builds.

A lot of the examples floating around are using a script that calls a script.  I do not to have to track another file for something like this, and I do not want to be messing with multiple languages in the same project.  I want it in Xcode, in the project settings, period.  And a lot of the default examples only show how to increment but not how to control when it happens.  Maybe I am picky, but I want the build number to increment on significant builds but not when I build the project seventeen times to debug something.

So here is the script that I came up with from Fredrik’s example and the comments on his post:

And where to put this? I am using Xcode 4.3.2.  From the Project Navigator, click on your project, then click on your target. Then click on the Build Phases. On the bottom right click on Add Build Phase, then Add Run Script. Drag the new section to right above Copy Bundle Resources. Then past the script above into it. And then check the box for Run scripts only when installing.

Seems to be working pretty well for me.  Ultimately I would like to get it where it combines a build number prefixed with the version number, but I did not have the time to figure that out.  If anybody else is doing it that way, leave me a comment.

Also the original script from Fredrik Olsson uses a separate variable, not the Xcode build variable.  If that sounds more appealing to you, check it out.

Xcode Archives Aren’t Showing Up In The Organizer

Xcode is currently becoming the biggest pain to work with since I developed a Blackberry app.  When I was trying to build an ad hoc distribution (which I still have not been able to get working yet), Xcode suddenly stopped showing my archives in the Organizer.  Having spent all day trying to get the ad hoc install working, I was am currently very frustrated with Xcode/Apple/iOS.  When my archives stopped showing up I really felt like I was not making any progress and that I was actually moving backwards!  I basically spent the past day trying to get back to where was yesterday.

I found a good list of solutions on Stackoverflow.  The most voted solutions did nothing for me.  But towards the bottom, I found one that fixed my issue.  Set this path to the value:

TARGET -> BUILD SETTINGS -> INSTALLATION DIRECTORY = /Applications

The actual value that I use is $(LOCAL_APPS_DIR), which is the default value that I found in some of my other projects.

I never changed this value myself.  There was no mention of it in any of the ad hoc install tutorials that I was following.  Somehow Xcode change it.  Very frustrating. This is not a guaranteed solution to the problem for everyone, as obvious from the list of solutions and comments on the problem.  So be sure to check out that Stackoverflow excahnge if this doesn’t fix it for you.

Set the UIImagePickerController to still photos only

Working on my iPhone app today I came across the need to setup the camera to only take still photos, and therefore ignore videos and hide the video camera.  The Apple docs mention that to do this you just set the mediaTypes attributes to only [[NSArray alloc] initWithObjects: (NSString *) kUTTypeImage, nil]. Unfortunately it’s a bit of a pain to figure out just how to pull in kUTTTypeImage. Here’s how you do it:

  1. Add the MobileCoreServices framework to your project.
  2. In the header file that is used to create the UIImagePickerController, add #import <MobileCoreServices/UTCoreTypes.h>
  3. User cameraUI.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeImage, nil];

I hope that helps. You can also use kUTTypeVideo is you want to use only video and not include still images.

Do you need a quality satchel?

So I’ve had my Leather Back Satchel for about half a year now, and I have to say that I love it. It is most definitely heavier than I thought it would be… much heavier. But once I got used to it, it wasn’t so bad.

I wanted to post a longer article, but time has been strained lately, so I’m just post these pictures for now. If you have any questions let me know.

Where’s crontab?!

Are you running Ubuntu and trying create a cron job, and everywhere you look you see the command crontab, but command not found is all you get when you try to use it? Yeah, that one never stumped me either 😉

I’m was working on setting up a cron so that I can email blog posts into django-articles (gotta love this app), and everywhere I look it mentions crontab. Took me a while to figure it out, but it turns out that the command crontab is installed with package cron–at least on Ubuntu. So if you are missing this command, do an sudo aptitude install cron and then you should have your crontab command.

I hope this saves someone a little time and frustration. I know I was surprised that it took me so long to find out which package this command belonged to (I thought it was its own package actually, so that slowed me down a bit).