Turning a C program into an Objective-C program

Discussion in 'Mac Programming' started by jakee.stoltz, Oct 8, 2010.

  1. macrumors newbie

    Joined:
    Sep 17, 2010
    #1
    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.
     
  2. macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #2
    Describe the purpose of the program and how the user interacts with it.
     
  3. macrumors 6502a

    Joined:
    Jun 27, 2010
    #3
    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.
     
  4. macrumors 6502a

    Joined:
    Jan 15, 2007
    #4
    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.
     
  5. macrumors 65816

    Bernard SG

    Joined:
    Jul 3, 2010
    #5
    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 will give you a quick start in its chapter 8.
     
  6. macrumors 68040

    Joined:
    Feb 2, 2008
    #6
    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.
     
  7. thread starter macrumors newbie

    Joined:
    Sep 17, 2010
    #7
    Code:
    //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.
     
  8. macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #8
    So basically replace the scanf()'s with text fields. It doesn't seem like such a big chore.
     
  9. macrumors 6502a

    GorillaPaws

    Joined:
    Oct 26, 2003
    Location:
    Richmond, VA
    #9
    You might want to take a look at Apple's Currency Converter Tutorial Application 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.
     
  10. macrumors 6502a

    Joined:
    Dec 29, 2009
  11. macrumors 604

    chrono1081

    Joined:
    Jan 26, 2008
    Location:
    Isla Nublar
    #11
    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.
     

Share This Page