Back into Objective-C

Trying something new this month– I’m going to try and write a post a day which should be challenging to say the least. I’ve let GA sit dormant for too long, and I’m doing some fun things that deserve to be noted about (if only for my reference).  First off, I’m buried in Objective-C again for the first time in a good long while.  iOS8 is about to be dropped shortly (few weeks) and I think the last time I built a functioning app for iOS was back on version 3; At that point they were still dubbing it an “SDK”.

I’ve been following an e-book that these guys put out, iOS Programming: The Big Nerd Ranch Guide which is rather neat I think.  It’s covering a lot of things about iOS7 that I never knew and is really making it easy to forget about all the crap I learned for dev on iOS3– manual reference counting, myriad hidden API’s and the like.  I’m having a blast doing it.

Anyhow, back to it!

Calculator (CS193P)

…And per that last post, I finally have the code in the way that I want it.  I think I’ll eventually getting around to showing some screenshots of what’s happening with the memory leak, but I think I’ve gotten everything correct thus far.  I’m uploading the working code as an Xcode 4 project, without any sort of build data with it.  You may need to fire up your copy of Xcode and adjust the build settings to not use iOS 4.3 but provided the API doesn’t change too drastically, you should be good.  Grab that zip file, here.
Also, I’m leaving out the XIB files– I think you can grok your own interface design, no need to steal my binaries.  If you have any questions, I’d be happy to try and help you out but really…it’s all stuff explained in the lecture or in the Assignment 2 request sheet.

What I will do is expose a bit of code here that gave me the most frustration.  It was a class method designed to evaluate an algebraic expression by spawning a working copy of itself to sub the vales of the variables in and return a result.  It was a bit of work to get my head around abstractly, and I think the resulting code is quite efficient and nice (and doesn’t leak!).

+ (double)evaulateExpression:(id)anExpression usingVariableValues:(NSDictionary *)variables
{
double dubNum=0;
CalculatorBrain *workerBrain = [[CalculatorBrain alloc] init];
for(id item in anExpression)
{
if ([item isKindOfClass:[NSNumber class]])
{
NSNumber *workingNum =item;
[workerBrain setOperand:workingNum.doubleValue];
}
else if ([item isKindOfClass:[NSString class]])
{
NSString *workingString = item;
if ([workingString hasPrefix:VARIABLE_PREFIX]) {
NSString *subFromDict = [variables objectForKey:workingString];
[workerBrain setOperand:subFromDict.doubleValue];
}
else
{
dubNum = [workerBrain performOperation:item];
}
}
}
[workerBrain release];
return (dubNum);
}

Anyways, I’m proud enough that I’m heading on to Assignment 3 which implements a Graphing mode. Hopefully I’ll get the instruments post, as well as a bit of a video demonstration of the Calculator in action up here.