PDA

View Full Version : How to work with external functions in Obj-C




Filini
Oct 26, 2009, 08:27 AM
Hi guys, i can't find out how to work with external functions in obj-c,

In C:

#include<stdio.h>

//external function
int function1(int x, int y)
{
int sum;
sum=x+y;
return sum;
}

//main code
int main(int argc, char* argv[])
{
int a=5, b=10, aaa;
aaa=function1(a,b);
printf("Sum %d", aaa);
return 0;
}

How to convert this code in Objective-C???

Thx a lot.



robbieduncan
Oct 26, 2009, 08:40 AM
Objective-C is a proper super-set of C. You can do anything you can do in C. So a function call is identical.

lee1210
Oct 26, 2009, 09:20 AM
As robbieduncan stated, if you saved this file as a .m and compiled it as Objective-C, it would run just as you'd expect.

If you're wanting to do this in a more "Objective-C"-y way, you could make function1 a class method of a "utility" class. The signature in the class header would be:

+(int)sumInt:(int)a and:(int)b;

The implementation would be:

+(int)sumInt:(int)a and:(int)b {
return a+b;
}

Let's say this is in a class called mathUtils. You would utilize this method like this:


...
#import "mathUtils.h"

int main(int argc, char *argv[]) {
int x = 5;
int y = 6;
int w = 0;

w = [mathUtils sumInt:x and:y];
NSLog(@"The value of w is: %d",w);
}


-Lee

Filini
Oct 26, 2009, 09:29 AM
If you're wanting to do this in a more "Objective-C"-y way, you could make function1 a class method of a "utility" class.
-Lee
Exactly, I meant how to do from this function - class method for Xcode, thanx a lot Pal:)

to robbieduncan
Thx too

robbieduncan
Oct 26, 2009, 09:33 AM
Exactly, I meant how to do from this function - class method for Xcode, thanx a lot Pal:)

to robbieduncan
Thx too

It is worth noting that there is nothing wrong with using C functions in Objective-C/Cocoa code were appropriate. In fact there are some very good reasons why you might want to: primarily speed and code size. It is significantly cheaper to call a C function that it is to call an Objective-C method. And I believe that it will also result in somewhat smaller code (although this is unlikely to be an issue either way).

Whilst in this example the performance is unlikely to matter either way if you call the same method lots of times in a tight loop you will see significant performance advantages if you make it a C function instead of a class method...

lee1210
Oct 26, 2009, 09:40 AM
It is worth noting that there is nothing wrong with using C functions in Objective-C/Cocoa code were appropriate. In fact there are some very good reasons why you might want to: primarily speed and code size. It is significantly cheaper to call a C function that it is to call an Objective-C method. And I believe that it will also result in somewhat smaller code (although this is unlikely to be an issue either way).

Whilst in this example the performance is unlikely to matter either way if you call the same method lots of times in a tight loop you will see significant performance advantages if you make it a C function instead of a class method...

You weren't saying this at all, but i just wanted to say, for the record, that I wasn't trying to say that the "right" way to do this particular task in Objective-C was a utility method, but thought that was what the OP was angling at.

As for code size, my understanding is that in your code, passing a message will result in a call to objc_msgsend, with one additional argument above and beyond the arguments being passed to the method (a pointer to the object the message is to be passed to). Therefore, you're using a tad more stack space, but no more "code". Obviously after that initial call there's a lot more code that has to get executed for the message to be passed, but in terms of your code, i think the size will be about the same.

-Lee

robbieduncan
Oct 26, 2009, 09:51 AM
As for code size, my understanding is that in your code, passing a message will result in a call to objc_msgsend, with one additional argument above and beyond the arguments being passed to the method (a pointer to the object the message is to be passed to).

You are correct, that sounds about right.

cea
Oct 26, 2009, 09:57 PM
Hello,
I had the same problem before. But i got help from Lee. You do not need to convert C to Obj-C.
You can download the example from here (post #5) to see how it works (i think it is what you looking for):
http://forums.macrumors.com/showthread.php?t=809552