On Monday, I spent some time in the morn… 

On Monday, I spent some time in the morning polishing up the photoshop comps I’d been working on. There’ll be a more detailed full writeup on the process I went through with those once they’re totally finished. In the afternoon I turned my attentions back to the Cheapest app. My mission: to locate and fix a bug that prevented use of numbers after the decimal point when an iPhone’s localization settings dictate a comma instead of a period as the decimal separator.

There was already some cleverness going on in terms of localization: the following line of code made sure that where appropriate, the comma was used instead of the period:
decimalChar = field.formatter.decimalSeparator;
decimalChar is then appended to the current digits in the field. This is all well and good: people get a familiar decimal separator. The problem was that when they moved on to the next field, digits after the decimal were lost.

What a mystery. I should mention that these are really my first forays into objective-c, so I’m very much learning to read the syntax as I go. After some time spent reading documentation, and hunting through different parts of the program, including an introduction by Steve to the debugging features in x-code, I figured out the process the application went through each time a field lost focus.

When the user presses a key on the calculator keyboard built into the app, that digit is appended to a string variable (string of text). Only when the field loses focus, is that value mined for its numbers, and passed to a different variable in the local currency format. The problem was, the part of the program that stripped the numbers out of the text didn’t know what to do with a comma, and so digits after the comma were lost.

The fix was quite simple: change commas to periods, before parsing the string for its numbers. I didn’t need to worry about putting commas back in, because the currency-formatted variable that reads those numbers already knows about localisation.

So the problem was fixed with a single line of code, incidentally, my very first line of code to go into a published application.

And here it is:
title = [title stringByReplacingOccurrencesOfString:@"," withString:@"."];

Takehome lessons:

  1. doubleValue doesn’t know about commas or localisation
  2. x-code’s documentation feature is my friend
  3. spend time figuring out exactly where your bug is happening in the code
  4. use debugging tools to do this
  5. once you know where it’s all going wrong, the hard part might well be over