Skip to content

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.

RxCalc128x128.pngAroundĀ 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!

appstore.pngIf 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.

Categories: Development, Indie, iPad, iPhone.

Tags:

Comment Feed

No Responses (yet)



Some HTML is OK

or, reply to this post via trackback.

Generic inderal
Plavix vs alternative medicines
Seroquel xr approved indications in canada
Que es lamictal 100mg
Lowest prices for yaz at pharmacy
Order yaz
How to catch a man
Did she cheat on me
How to know if your wife is cheating on you
Can you buy ultram in mexico
Order ultram online
Ultram pill price
best cell phone spyware
gut feeling cheating
signs that your guy is cheating
sms spy
a cheating spouse
how to know your partner is cheating
signs he cheating
signs of cheaters
spy cheating spouse
Tramadol Online
buy 100mg tramadol online
Strattera prescribing information
Purchase strattera
Vpxl without a prescription or doctor
Herbal form of viagra
how can i find out if my husband is cheating
signs of cheating spouse
when your husband cheats
where to buy metronidazole
toradol shot
buy tramadol cheap no prescription
cheap non prescription tramadol
cheap tramadol without prescription
cheap doxycycline tablets
generic cialis
buy tramadol online
buy synthroid online
order tramadol online
buy vibramycin
buy toradol
buy lidocaine
buy celebrex
buy flagyl
lasix furosemide
canadian family pharmacy
cialis online
tramadol 100mg
nexium generic
buy nolvadex
how to catch a cheating wife
generic viagra
Canadian Family Pharmacy
buy nolvadex