More Swift – A minor refactor

In my earlier I post I talked about not being very sure that a method I wrote for one of the Stanford iOS Programming course assignments was really embracing Swift as a language. I then posted about using Swift’s unit test framework that would make any refactoring easier and better.

And so on to the refactoring…

Well, with a fresh day came a fresh eye, and there was not too much I could really do to make the method more Swift than C/C++. I ended up mainly folding some let statements into inline expressions. Not exactly hard, but when all was completed I think the code is more or less as tight as it could be and still easily readable – opinions to the contrary are very welcome.

Continue reading

Using Swift’s Unit Test Framework

In an earlier blog post I wrote about my solution to one of the homework assignments for Stanford University’s iOS programming course. My conclusion was that, while the code worked, it seemed a little bit too C-like and not really embracing some of the elegance of the Swift programming language.

My intention is to rewrite the code and see if I can get it looking somewhat more Swifty. But of course in doing that I don’t want to break it – there’s a lot of different cases the CalculatorBrain has to handle. This is a classic scenario that can be solved using automated unit testing.

When you create a new application project in XCode, as well as the main build target you get given a test target with boilerplate code for using XCode’s unit testing framework. It is simple to use – just add new functions to the test class, and use XCTAsserts to ensure the right results.

Continue reading

Backing up using Carbonite

At home I have a fairly good backup scheme for ensuring my main PC and the wife’s laptop have their critical data stored on at least a couple of other disks. In addition to using cross-copying each other’s content, I have a second hard disk in the main PC just for backups and an external USB disk for backing this up to. I also have a few hundred megabytes of useful everyday stuff synced across machines and into ‘the cloud’ using the excellent Dropbox utility. So I’m pretty well covered against hard disk failure, and my critical files on Dropbox are insurance against houshold disaster.

Yet I’ve always had this nagging feeling at the back of my mind about the 80+Gb (and growing) of other data such as photos, videos, and general stuff that falls under the category of don’t-look-at-much-but-don’t-want-to-lose. While the hard disk duplication may cover me against hardware failure, and in fact did so a couple of years ago, I retain a nervousness over fire, theft, flood, or enormous wine spillage in my IT area.

Continue reading

A simple Perforce backup script for the home

I use the free license of Perforce on my home network just to ensure version control and management of home coding projects, university thesis stuff, and general files that I just want to ensure I have previous versions of. It’s a really nice part of the Perforce licensing model that I can get two users and five clients for free. My network has my main PC (with Perforce depot), my personal laptop, my wife’s laptop, and my work laptop all connected. It’ll get worse when my four-year old starts wanting to source control things from his PC!

Having been responsible for some pretty heavyweight Perforce installations in my working life, I did not want to forget the golden rule of checkpointing and backing up. While it would not be a disaster if my Perforce data got lost or messed up, it would be an inconvenience. The scripts published on Perforce user forums, however, tend to be focussed on industrial installations and can be quite complex. For the home it is nice to have something more simple.
Continue reading

Plotting your GPS tracks in Google Earth

Google Earth

As a follow up to my previous post about using Google Maps in conjunction with your own GPS data, you can also do the same thing in Google Earth. Something I missed before was that the same GPSVisualizer site provides the service so that you upload your GPS file, and it generates a Google KMZ file for you. Click on it, and the file gets downloaded and launched in Google Earth directly. Very neat and very easy. Here’s a screenshot:
Continue reading

Playtime with Google Maps and GPS

I’ve been dabbling with Google Maps recently as I want to download some GPS data from my PDA to visualise and analyse journeys that I have made. I also think Google Maps and its 3D sibling Google Earth are the absolute dogs doodads of applications and so any excuse to play with these was welcome.

I started with a .GPS file that was generated automatically by Copilot, which is the in-car satellite navigation software that I use. It’s pretty good, but like all Satnav systems you need to be aware of its foibles to get the best out of it. Never, ever, simply follow the instructions!

The GPS track was a record of my travels on Monday 25th June as I tried to get into work in Sheffield as it was getting hit by freak floods. Getting in was OK(ish), but took a long time. Getting home again was a bit more interesting as I kept trying to avoid all the bad traffic.

Anyway, since I was being forced to turn around and try new routes to avoid flooded roads, jams etc, I wanted to see on my PC where I had been. What I did not know was a quick and easy way to do it.

Copilot has a PC hosted planning application, but that won’t take the GPS tracks that the PDA version generates (sigh….). Ideally I wanted to put the tracks into Google Earth to look at in glorious 3D, but I currently only have the free version that does not allow GPS tracks to be imported.

A quick webhunt brought up GPS Visualizer. This provides a free service that allows me to upload the GPS file, and it generates a Google Map with a vector overlay of the results. Even better, the site contains instructions on hosting the resulting map on your own website.

So I had a go at this. The HTML pathway was a huge faff, to be honest, as I was rather foolishly trying to use Frontpage. The problem was that it was trying to be too smart on the Cut and Paste operation, which screwed HTML tags over. In the end, a quick path was to:

  1. View GPS Visualizer page source
  2. Save source to local HTML file on disk
  3. Open HTML with an editor. Even Frontpage could manage this bit!
  4. Locate the Google API key, and replace it with my own (see below).
  5. Set page titles, modify the default 600×600 window
  6. Upload to my website
  7. The resulting page can be seen here.

    If you want to do this yourself you’ll need a Google API key, which you can get from here

Tips for returning IDENTITY values from INSERT

There’s a good article by Scott Guthrie here that describes the basics. Just scroll down to Tutorial 5 for the INSERT specific bit – it is pretty straightforward and there is no point my repeating it here.

One point to note, however, and why I wrote this particular post. The key step in getting this working is changing the query type from NonQuery to Scalar. But this can get reset by Visual Studio if you choose the Configure option on the ObjectDataSource’s smart tags to modify the query in any way. If you don’t notice this, then suddenly the identity value stops getting returned and your code breaks.

The second tip is that by default the type returned by the insert query is decimal. This can cause some extra casting in your code if you were expecting it to be int. To fix this, just modify the SQL in Scott’s original article to be this:


Perforce server logging problem

When setting up a server it is often useful to enable one of the logging levels. The admin guide states that you need to do this:

  • p4 set P4DEBUG=server=2

which sets the server logging level to 2. You then need to restart the Perforce server.

However, if Perforce is running as a service (the default under Windows), then this does not work. The reason is that “p4 set” only sets the variable in for HKEY_CURRENT_USER in the Windows registry. However, when running as a service, p4d picks up its environment variables from HKEY_LOCAL_MACHINE. To do this you need to set the ‘-s’ flag. E.g.

  • p4 set -s P4DEBUG=server=2

Streamlining your PC to play games


Before I run a resource intensive game, I execute a little batch file that kills a bunch of background processes that are simply not needed when playing the game. I’ll explain the details below.

I have also noticed that before I used to do this, I experienced awful crashing problems with my PC just locking when playing. Since killing these extraneous processes, however, I have not had a single in-game crash that I used to get regularly with both BF2 and BFV. I think this shows that something in my system is having a bad interaction with the game – probably the graphics driver. I used to think it was an interaction with Panda antivirus, but recently I modified the script to not kill Panda and things still run OK. This makes me a little happier that I can keep my AV program running at all times.
Continue reading

Gotcha on setting up a data connection

This one gets me every time. When you set up a new data connection to a database running on a local copy of Sql Server Express, in the “Add Connection” dialog you need to prefix your machine name to the defaulted value of “SQLEXPRESS”. E.g. if the network name of your PC is called “devpc”, then the connection string is:


While this is the obvious choice and works for many circumstances, a subtle variation is to use the following instead:

  • localhost\SQLEXPRESS

This now points the connection to an instance of SQL Server Express running on your local machine. If working in a team where each developer needs their own local copy of a database, then setting this in the web.config file can be a real help.