Re: Integrating a C command line tool into Objective-C application
Re: Integrating a C command line tool into Objective-C application
- Subject: Re: Integrating a C command line tool into Objective-C application
- From: Jason Wiggins <email@hidden>
- Date: Sun, 9 Sep 2007 13:27:03 +1000
On 08/09/2007, at 7:15 PM, Uli Kusterer wrote:
I hope I'm not explaining too basic things to you, but I'm just
trying to cover all places where you could be hung up:
Basic is good as I'm learning this "new" stuff.
A function in C is essentially the same as a function in ObjC, e.g.
NSLog() is a function. Some C compilers let you put one function
*definition* (1) inside another, which is what is called "nested
functions". This is a nonstandard extension that e.g. GCC 3.3 would
let you do it. GCC 4.x doesn't let you do nested functions anymore
(2).
Methods are very similar to functions. Essentially, they are
functions associated with a class of object. If you try to put a
function inside a method, you'll probably get the same error message
as when you try to put one function inside the other.
Something like this must be going on in the code you created based
on your colleague's code. Either you copy-pasted a group of
functions into one of your methods, or your colleague used nested
functions.
That's what I did, just copy and paste job, just to see if it would
work, but no.
There are two approaches I usually take when converting other
people's C code to ObjC:
1) Take the whole C code and just write a function that effectively
contains the code from the "main" function, but with nicer
parameters, and then call that C function wherever I need.
2) If I want to replace some code, or the code calls back to my code
(in C this is usually done by passing a pointer to one function to
another), I just turn the whole package into one class, and make
every function a method. The callback functions get turned into
methods that simply call through to a delegate I add to instances of
this class.
Of course, if I have to keep the original C code and my Objective C
code synched, I usually take a hybrid approach, where I generally
just do #1, but create an ObjC class that calls the function that
was based on the "main" function, which installs callback functions
that just call through to a delegate.
Cheers,
-- M. Uli Kusterer
http://www.zathras.de
1) A function definition is essentially the code that makes up the
actual function. This is different from a function call, which is
what most function definitions use to actually do their work.
2) The way GCC implemented nested functions was taking advantage of
a security gap in the OS, which Apple intends to plug, so they
removed this feature so people can prepare. Nested functions are an
extension, not part of ANSI standard C or ISO C, so the complaints
weren't too loud.
Thanks for the info, I'll have to read it a few times to let it sink
in. I'll take what you have provided and give it a go.
Regards,
Jason Wiggins
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden