When I set out writing this blog, I wanted to ensure that I was writing some code every day, even if only a little. Work has been slow lately, so this hasn’t been too hard to accomplish. Sadly, I’d also expected to be making more progress in a day than I accomplished today. Still, perhaps the failures are as worth recording as the successes.In this case, I was setting out to knock off some more of the bugs that afflicted my initial implementation. You’ll recall that while I just recently enabled the ability to calculate something on any line, I also had a few other problems:

  1. I can’t put spaces in an expression
  2. Crap characters appear after results in some cases
  3. After doing one math result, it does no more
  4. I want cool animations!

Number 4 is going to have to wait a while, I think. After that post, the awesome Mike Piatek-Jimenez wrote with a bit of advice regarding item 1: my NSRange is probably not correct. And indeed, that turned out to be the case. In my calculate: method, I had the equal sign that triggers the method output the “=” into the text view prior to doing the work of gathering the calculation. And then I was screening it out when I selected my bit of text! Stupid. That helped a lot, actually, taking care of both issues 1 and 3. So now I can run all the calculations I want.

Interestingly, I’m having a semi-issue in that NSLog continues to stop working after the initial calculation. I’ve got my calculator method echoing out the keyboard input, but that stops occurring after that first time you hit “=”.

And the junk characters of item 2? This is very much still an issue, and it appears more pernicious than I first thought. In fact, the type and number of characters that appear afterward seems random and spiteful, if not downright capricious. It suggests to me that some extra memory is being tacked on. Here’s a video of the current Napkin in action:

Take note of that final calculation, where it spits out a pile of crap after the answer. Here’s a funnier thing. I go back to that last answer, delete everything to the equal sign and hit “=” to trigger the calculation. Here’s the screen now:


You can’t tell me that’s not some other bit of memory getting all up in my business! My suspicions right now are falling on the bytes: method of NSData, which is responsible for converting the NSData output of the pipe which provides the calculation into an NSString:

NSString *result = [NSString stringWithFormat:@”%s”, [stdOutData bytes]];

If I run the NSString method length: on result, I get not just the number that mybc provides, but the junk on the end as well!

All this stuff is very interesting, but I’m still not quite sure where to head next. I’ve got an offer from a fellow Tacow-er to have a look at my code, so I’m hoping he’ll be able to help. In either case, I’ll be banging away at this until it’s working. Stay tuned…

Update: A few hours later, and some searching through Cocoa Dev has turned up an answer to the junk data. Turns out that I wasn’t implementing my conversion from NSData to a string correctly. Here’s the correct code:

NSData *stdOutData = [[[calcTask standardOutput] fileHandleForReading] readDataToEndOfFile];

And with that, no more junk data. I think I just earned myself another episode of The Wire for that. Catch you tomorrow!