PDA

View Full Version : Turning a C program into an Objective-C program




jakee.stoltz
Oct 8, 2010, 11:26 PM
Hey all. So I realize this is probably a question without a definitive answer but I just thought I'd see if anyone has any advice on the matter.

I wrote a program in C. Pretty basic, just does a bunch of calculations that I need to go fly. Where would you start in converting this to an Objective-C program?

I've started reading "Programming in Objective-C" so I'm starting to understand the language a bit. It's definitely a different way of programming so I'm kind of lost on where to even start with all these methods and classes and etc.

Ultimately, I'd like to turn it into Mac program with a simple graphical interface.



lloyddean
Oct 9, 2010, 12:07 AM
Describe the purpose of the program and how the user interacts with it.

SidBala
Oct 9, 2010, 12:11 AM
Basically, you want to convert your functional code into an object oriented one.

Show us some code and tell us how complex/simple your program is so we know what we are dealing with here.

foidulus
Oct 9, 2010, 03:28 AM
Not a whole lot of point in doing it really. Speed isnt necessarily objective-cs strength, due to the way it processes messages etc it is actually slightly slower than c apps on average.

If all you want to do is add a Cocoa gui then its pretty simple to do so without changing any of your code, after all objective-c is a superset of c.

Bernard SG
Oct 9, 2010, 03:43 AM
Theoretically you don't need to "convert" a C program into Objective-C. C code is valid objective-C. Practically, of course, this will be nuanced according to what your program actually does and how it is structured.
Whatsoever, if your target is just to make your program function inside a Mac OS App UI, the answer to your question "where to start?" is to get a thorough understanding of Xcode, and particularly how a Cocoa Project is structured, how to use the Interface Builder, how it links with the objects in your Objective-C Code. Fortunately, it is not complicated and that's what makes Xcode such a great tool.
Once you get decently acquainted with that it shouldn't be to hard for you to figure out what you want your final product to look like and how Xcode will get you at that.
The main role that Objective-C will play will be of an interface between the UI and your C code, which I presume is mainly functions, as you mentioned it's about calculations. So it's about transforming user's input into C functions arguments and then sending back whatever your function returns to an UI outlet.

Assuming you're unfamiliar with all the above, this book (http://www.cocoalab.com/BecomeAnXcoder.pdf) will give you a quick start in its chapter 8.

subsonix
Oct 9, 2010, 10:20 AM
Most likely you can just drop your C functions inside an objective-C class and have them work as methods. One issue can be that some basic data types of C is not always 1-1 compatible with cocoa classes, but you can use corefoundation datatypes to encapsulate them, CFString and so on.

But your first step should probably be to learn Xcode, interface builder and the basics of cocoa and obj-c.

jakee.stoltz
Oct 9, 2010, 11:30 PM
//Get user input (altimeter setting and field elevation and temps)
printf("Altimeter setting at your departure airport: ");
scanf("%lf", &s);
printf("Field elevation at your departure airport: ");
scanf("%lf", &f);
printf("Temperature at your departure airport (degrees Celsius): ");
scanf("%lf", &atg);
printf("Cruise altitude: ");
scanf("%lf", &pa);
printf("Temperature at your cruise altitude: ");
scanf("%lf", &at);

switch(zz)
{
case 0:
printf("\nEnter the values for %.0f feet \n", paas);
printf("Time: ");
scanf("%lf", &ft);
printf("Fuel: ");
scanf("%lf", &ff);
printf("Distance: ");
scanf("%lf", &fd);

printf("\nEnter the values for %.0f feet \n", pa);
printf("Time: ");
scanf("%lf", &ct);
printf("Fuel: ");
scanf("%lf", &cf);
printf("Distance: ");
scanf("%lf", &cd);

if(atg == stg + 10.0)
{
ft = ft * 1.1;
ff = ff * 1.1;
fd = fd * 1.1;
}

if(at == st + 10.0)
{
ct = ct * 1.1;
cf = cf * 1.1;
cd = cd * 1.1;
}

break;

case 1:
printf("\nEnter the values for %.0f feet \n", paas);
printf("Time: ");
scanf("%lf", &ft);
printf("Fuel: ");
scanf("%lf", &ff);
printf("Distance: ");
scanf("%lf", &fd);

printf("\nEnter the values for %.0f feet \n", lpa);
printf("Time: ");
scanf("%lf", &ct);
printf("Fuel: ");
scanf("%lf", &cf);
printf("Distance: ");
scanf("%lf", &cd);

printf("\nEnter the values for %.0f feet \n", hpa);
printf("Time: ");
scanf("%lf", &hct);
printf("Fuel: ");
scanf("%lf", &hcf);
printf("Distance: ");
scanf("%lf", &hcd);

//Function to add 10% if the temp is +10 of standard
if(atg == stg + 10.0)
{
ft = ft * 1.1;
ff = ff * 1.1;
fd = fd * 1.1;
}

if(at == st + 10.0)
{
ct = ct * 1.1;
cf = cf * 1.1;
cd = cd * 1.1;
hct = hct * 1.1;
hcf = hcf * 1.1;
hcd = hcd * 1.1;
}

ct = ct + (lpa-pa)/(lpa-hpa) * (hct - ct);
cf = cf + (lpa-pa)/(lpa-hpa) * (hcf - cf);
cd = cd + (lpa-pa)/(lpa-hpa) * (hcd - cd);

break;


That's a small chunk of code. There are two more cases in that switch alone. Basically that sums up the purpose of the code though. I have the user input certain values, test those values for certain conditions, have the user input more values based on those certain conditions, apply the appropriate formulas, and display results.

Your right in that my goal isn't really to convert this to Objective-C, it's to put a graphical interface to the code and make it shareable. I just figured I had to know Objective-C to use Cocoa.

I'm working on understanding Cocoa and Objective-C as we speak. Recently joined the Apple Developer program as well. I've been using XCode for a while now to code in C so I'm familiar with that part at least.

jared_kipe
Oct 10, 2010, 12:27 AM
So basically replace the scanf()'s with text fields. It doesn't seem like such a big chore.

GorillaPaws
Oct 10, 2010, 04:58 AM
You might want to take a look at Apple's Currency Converter Tutorial Application (http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjCTutorial/01Introduction/01Introduction.html%23//apple_ref/doc/uid/TP40000863) where it walks you through creating a very simple Cocoa app. The finished project will likely look pretty similar to what you're shooting for in your app.

Depending on how far you've gotten in your Objective-C studies, it should be relatively straightforward. Feel free to ask us for help along the way if you run into problems.

If things really don't make any sense at all, it could be a sign that you're getting a bit ahead of yourself in the progression of your studies. In that case, I would recommend slowing things down, finishing the Kochan book and then come back to the currency converter tutorial.

Blakeasd
Oct 11, 2010, 05:15 PM
Printf would be replaced with NSLog

chrono1081
Oct 11, 2010, 10:09 PM
This shouldn't be hard at all. Its been awhile since I went through the Objective-C book but I bet you could make that if you only finished the first 3rd of the book (as long as it was console only). For graphics I'd say finish the book and learn some Cocoa basics and you'd be set.