Last time, I was trying to get a shell script to give me back the results of a calculation sent to the command line utility bc. By writing the shell script that takes an argument, like

mybc “2+2”

I get back the right answer:


Amazing. A computer doing math! In my previous post, I bitched about how hard NSPipe is to use, so I was avoiding it. Turns out, you can’t do that. I found this great little page that explains very clearly how to use a pipe to get data back from an NSTask. I basically ripped the whole thing off into my own code. Isn’t that how this stuff works? Amateurs borrow; professionals steal.

At the end of it, I have a means for my Calculator class to be alerted when the user hits the equal sign, take a string (at present, I’ve hard-coded the string “2+2”) and insert the solution after the equal sign.

An interesting side note about my experience with Cocoa so far. There seems to be a lot of time spent converting one object type to another. Take this latest issue for example: NSTask requires an NSPipe, for which I need an NSFileHandle to read from it. I then need to get the contents of that file handle, which comes in the form of an NSData object. Finally, I get the results of that by converting it into an NSString object, which is what gets passed back to my text view. I reckon that after I do this long enough, these data transitions will become second nature. Meanwhile, I’m spending a lot of time with my nose in the documentation, assembling a chain of events that will turn input A into output B.

For my next trick, I need to put together a means to have everything to the left of the equal sign get sent to my Calculator class. I think I’ll be using NSRange to accomplish this, but I’ll need some way of parsing that text. See you on the other side…