Sunday, December 30, 2012

Care when posting from the web

I've been working on a new application (Electronic Detective for my daughter) which will be the basis for a longer-term game effort and ran into a strange thing. I decided that how I was going to be doing the UI for a particular portion of the game was just incredibly clunky. I had remembered a piece of information that needed to be included and rather than shoehorn it in, I decided to take a step back and see what made the most sense. In this case it was changing things from a static UIView to a UITableView. This opened up a lot of potential real estate and made it possible to do some things I was struggling to fit on the screen before.
I was searching to find the best way to get UITableViewCells working with the new Xcode (4.5) since they've sort of changed how they operate since last I'd done one (a long time ago - in a galaxy far, far away). I found a good sample from a blog post and decided to just copy/paste and modify the sample code. And things were fine - until I got a very strange build error - "unexpected '@' in program". Um, whiskey tango foxtrot was that? I searched a bit more and the answers proposed didn't seem to really address my particular error. Here was the offending code (from a code blog I found):

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
 static NSString *CustomTableCellIdentifier = @”CustomTableCellIdentifier”;

 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CustomTableCellIdentifier];

 if(!cell)
 {
   NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@”CustomCell” owner:self options:nil];

 if(nib.count > 0)
 {
   cell = self.customCell;
 }
 else
 {
   NSLog(@”failed to load CustomCell nib file!”);
 }
 }
 
There didn't really seem to be anything wrong. The highlighted lines were the ones causing the problem and I couldn't really see a problem. Each of the literals was perfectly correct. What the hell was going on with this code? Then I stumbled upon a stackoverflow question and answer that provided a little insight. What was weird about the editor was that it wasn't showing the text formatted as "text" - it was just showing as black text, not the typical red you'd see. Then I read the part about the "smart quotes". I replaced the first "quote" with a regular "quote" and the text higlighted. That's when I realized I must have pasted in smart quotes and that's what was causing the problem.
I replaced all six quotes and things started working the way I expected. So, as a word of caution, Xcode apparently will maintain some special characters when you paste code from other sources. Make sure that if you're pasting that you double-check the characters. If things don't look correct to you, they probably aren't. It may be a bit weird, but it may be worth your effort to retype some of it manually in the editor to see if it's a problem with the paste or with the code. You could save yourself a little mental anguish by trying a couple of simple things. Good luck and happy coding.