C++ to Objective-C

About year ago I started looking at writing my first iPhone application. My brother and I had decided we’d finally go ahead and create an application we’d had many fits and starts on but had never finished. The code started its’ life on Palm and Windows. Back when we started I’d written the math code in portable C++ so we could use it on many different platforms.

AroundÂ Thanksgiving I’d put together a prototype of our iPhone application using that C++ code. I started by creating an iPhone Cocoa View Based Application, and added my existing C++ classes to it. That was pretty simple, now to compile the code. This is where I ran into my first problem.

Say What?

The Objective-C compiler didn’t like having a mix of C++ and Objective-C. Oh, I thought that was allowed? It is. You simply need to name your source files with a .M or .mm extension instead of the standard .m for Objective-C. That was easy.

Below is an example C++ class used in an Objective-C class. The PKMath namespace is where the C++ types and classes live. PKMath::VancoBolusDose() just worked. I verified that by viewing the instance vancoBolusDose in the debugger, so I knew we were good to go with a mix of C++ and Objective-C.

- (IBAction)calculateNewHandler:(id)sender
{
double height = heightTextField.text.doubleValue;
double weight = weightTextField.text.doubleValue;
double age = ageTextField.text.doubleValue;
double scr = scrTextField.text.doubleValue;
double peak = peakTextField.text.doubleValue;
double trough = troughTextField.text.doubleValue;
PKMath::Gender gender = (0 == genderSegment.selectedSegmentIndex) ? PKMath::kMale : PKMath::kFemale;

// Create an instance of a C++ VancoBolusDose.
PKMath::VancoBolusDose* vancoBolusDose =
new PKMath::VancoBolusDose(weight, height, age, scr, peak, trough, gender);

// Do stuff with dose here.
delete vancoBolusDose;
vancoBolusDose = NULL;
}

So, along comes December, and I haven’t really done much with the iPhone app. I was thinking about it, but not writing much code. I wanted to mess with Objective-C more, try some things out. I decided I’d port the PKMath classes from C++ to Objective-C. It took very little time and I had the beginnings of the PKMath library used in RxCalc 1.0.

Revisiting the code example above, this is what it looked like after creating the new Objective-C version.

- (IBAction)calculateNewHandler:(id)sender
{
double height = heightTextField.text.doubleValue;
double weight = weightTextField.text.doubleValue;
double age = ageTextField.text.doubleValue;
double scr = scrTextField.text.doubleValue;
double peak = peakTextField.text.doubleValue;
double trough = troughTextField.text.doubleValue;
PKGender gender = (PKGender)genderSegment.selectedSegmentIndex;

// Create an instance of an Objective-C VancoBolusDose.
VancoBolusDose* vancoBolusDose =
[[VancoBolusDose alloc] initWith:weight :height :age :scr :peak :trough :gender];

// Do stuff with dose here.
[vancoBolusDose release];
}

Apple Core Labs first product, RxCalc, was accepted in the iTunes App Store on July 4, 2009. What a great day!

If you’re a Clinical Pharmacist, and perform Pharmacokinetics calculations on a daily basis you can purchase RxCalc for the very low price of $0.99.