Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C C Style Functions

Tags:

c

objective-c

When doing objective-c, do I have to write out functions like:

- (int) someFunction: (int) a someParam: (int) b;

Or can I use regular C style:

void someFunction(int a, int b);

If I can do the C style, is there any benefit to doing it in the Objective-C style?

like image 820
Martin Blore Avatar asked Nov 30 '22 23:11

Martin Blore


2 Answers

You can use regular C functions, if you like, since Objective-C is just a super set of C.

You need to use Objective-C syntax if you want to use Objective-C features, like classes, messages, inheritance, etc... and of course to use the Cocoa/CocoaTouch SDK.

You can mix Objective-C and C code in your files, and have an Objective-C method call out to a pure C function. You can also call an Objective-C method from a pure C function by accessing directly the Objective-C runtime layer (e.g., using the objc_msgSend function).

like image 75
sergio Avatar answered Dec 05 '22 00:12

sergio


That's not an Objective-C function, it's a method. It's got access to an instance's variables and other methods. The two are not equivalent. In most cases you should use an Objective-C method. Why do you want to use C functions? What are you trying to do?

like image 27
Jim Avatar answered Dec 05 '22 01:12

Jim