Making the Left Side of = My Bitch

That’s really what it comes down to. Whatever the user has entered into Napkin, if they hit the “=” sign, I want to snatch up the equation they put in and spit back an answer. That’s easier said than done, since there are a lot of situations that I have to contend with. Some that occur to me include:

(2+3)/7 = My age is 2008 – 1973 =

For now, I’m only going to be dealing with the first example, where the only thing on a line is the calculation. You want a new calculation? Go to a new line. Perhaps it won’t be too onerous to screen out non-equation-type characters before I send it on to the Calculator class. We’ll get there.

My suspicion about using NSRange to get the string in question was pretty good, it seems. I went right to page 332 of Hillegass (3rd Ed.), and my code looks like this:

// Get everything on this line to extract the expression NSRange aRange; NSString *newline = @”\n”; aRange = [[self string] rangeOfString:newline options:(NSBackwardsSearch)];

The sharp-eyed will immediately spot a problem with this: this code searches from the end of the NSTextView. That’s fine if you’re typing at the end of the document, but what if you go back to an earlier line and enter this equation? Well, you’re fracked, that’s what. So I’m obviously going to have to find a way to do this backwards from the insertion point. We’ll get to that later, right?

Side Note: I got stuck trying to figure out how to get an NSString out of an NSTextView! As you can see, I used the string: method on NSTextView, but it’s not documented! I found it mentioned on the Cocoa Dev mailing list as a “convenience method”, whatever the hell that means.

Okay, so with the provisos I’ve mentioned, that code above works in getting me a correct NSRange struct that tells me where I need to start getting a string into my Calculator. But wait… how do I actually use that NSRange?

I’m digging through the NSString documentation, certain there should be a class method like +stringFromString:range: but there’s nothing of the sort. So I Googled for “NSString from NSRange” and got nothing remotely useful. I also went through the CocoaDev archives without any luck.

So what’s a hopeless nerd to do? Try Twitter, of course (hi, I’m @aaronvegh). For some crazy reason, after this latest C4 conference earlier this month, a metric whack of really smart developers started following me; I almost feel sorry for the poor bastards. But they’re useful!

aaronvegh: Why isn’t there a -stringWithString:range: method? jxpx777: @aaronvegh If only there were a way to add missing methods to classes in Objective-C… pilky: @aaronvegh [@”some string” substringWithRange:NSMakeRange(2, 4)]; mikepj: @aaronvegh try [NSString substringWithRange:] bwebster: @aaronvegh How about -substringWithRange:? nedley: @aaronvegh -substringWithRange:

Hot damn! When Jamie Phelps’ reply came in, he was heading in a direction I was already thinking about: adding a Category to NSString to deal with this. Since my first scan through the docs yielded nothing, I thought I must be approaching this issue incorrectly; if I couldn’t find the method I was thinking of, well obviously it’s because there’s no need for it! Hey, I’m a n00b.

But substringWithRange: is frickin’ exactly what I’m after here. And you know what? Works like a charm!

NSRange thisLine = NSMakeRange(aRange.location, ([[self string] length] – 1)); // the “=” is not part of the expression NSString *expressionString = [[self string] substringWithRange:thisLine];

Launch the app, enter a mathematical expression, and hit the “=” key. Boom! The answer appears.

Well, there are other issues now:

  1. When I put spaces in the expression (like “12 + 13”), it craps out. “12+13” is fine.
  2. For some answers, an upside-down question mark (like you see in Spanish) appears on the next line. That’s bizarre!
  3. What if I go back and edit the expression? That’s right, nothing.
  4. The math only runs once: the next line I go to, and it stops capturing input, and doesn’t do any more calculatin’.
  5. I want cool-looking animations to appear when the answer pops up!

As you all know, the difference between a Mac app and a Windows app, is that the former has style and panache, whereas the latter is a total hack job. On the other hand, I’d be shipping right now if this were Windows. In truth, there’s a lot of cleanup to do before this feature set is usable.

Stay tuned…