Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Mixing pure C and Objective C

davo666davo666 Posts: 65Registered Users
edited March 2009 in iOS SDK Development
hello,

in a C function i have these lines:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"Titie" delegate:nil cancelButtonTitle:@"Done" otherButtonTitles:nil];
	[alert show];
	[alert release];

the error i get is 'self' undeclared, but what i need to know is how do i get this C function to be in the scope of 'self' (im a c coder so: what the hell is self anyway?!? :) )

thanks for you help
Post edited by davo666 on

Replies

  • zhylazhyla Posts: 253Registered Users
    edited March 2009
    I don't see self in the code listing, so I'm confused by your error. Sure you got the right lines?
  • RickMaddyRickMaddy Posts: 2,122New Users
    edited March 2009
    If you are going to write apps for the iPhone then you MUST learn Objective-C. 'self' is used within a class to reference the current instance of the class. None of that seems relevant to the code you posted.

    Exactly what line that you posted is giving you this error? You need to provide more details.
  • jtarajtara Posts: 406Registered Users
    edited March 2009
    You can't "mix pure C with objective C" like this.

    Not sure if you have some functions in a .c file, or simply a C function (not a method) within a .mm file, since you haven't stated. But, no difference, really.

    If you have a pure C function you can't just plop some Objective-C in the middle of it. I know this is confusing because, of course, you CAN just plop some plain-old C in the middle of some Object-C code.

    That is because Objective-C simply adds some features to C. NOT the other way around! ;)

    I believe that there is a way to CALL an Objective-C method from within a C function, but I haven't done that so I don't know the details.

    "self" is a bit of the magic that makes Objective-C "objective". Every Objective-C object has an implied variable called "self". You don't declare it - it is declared for you behind the scenes. It is a pointer that points to the object's data storage block.

    This is part of the thin veneer over C that is Objective-C. When you call an Objective-C method, the "self" variable of the method's object is a hidden, implied first parameter.

    If you want to see some really goofy C code, take a look at the IJG JPEG library. (Which will be found under the hood of most JPEG implementations.) It's object-oriented code written in pure C. It's both ugly and elegant. They have to carry all those "self's" around explicitly. Yuck. :(

    The best way to use pure C with Objective-C is for simple functions that are passed a parameter (or parameters) and return a result. Treat C code as a separate, black-box blob. It's great that we can use pure C, because we can leverage legacy libraries. Particularly nice if you have some complex math for which there is a well-tested workhorse library or function available.
  • davo666davo666 Posts: 65Registered Users
    edited March 2009
    sorry the code block references the delegate 'self' (i had nil in there for testing)

    -- basically what happens is a have a C library that is event driven in a way that: when something occurs it calls a C function to tell it that.

    i want the C to then notify the objective C however i am quickly realising that this isnt possible :(

    so a better question is: would it be stupid to have an NStimer poll a flag that the C library sets if it has something to tell it?

    or even better: is it stupid to have a 2 second nstimer running the whole time?

    thanks :)
  • davo666davo666 Posts: 65Registered Users
    edited March 2009
    ohh! i founf this while poking around on the net:

    extern "Objective C"

    could I declare a method extern and thus have the C function 'see' it

    btw - i can call class methods (the ones with the +, lol - im not good at objc)
    but what can class methods really do in objective C? i mean i cant then do a 'self' inside that either (i just want to display an alertsheet, but this i think means getting access to the object)

    hmm its very difficult...
  • jtarajtara Posts: 406Registered Users
    edited March 2009
    Well, I was wrong. You CAN just plop some Objective-C in the middle of a C function! See here:

    Re: How to call objective-c method from c ?: msg#00099 lib.gnustep.general

    But, of course, then it is no longer pure C code, and you are compiling it with the Objective-C compiler. But for your purposes, probably fine.

    The purpose of class methods is the same as in C++ - to do something specific to the class that is NOT specific to a specific object of that class. For example, alloc is a class method. It can't possibly be an instance method, since it's purpose is to create an object. So there is not yet an instance.

    Methods related to reference-counting are class methods - they keep track of how many objects of a given class you have, so they cannot be associated with a given object.

    Although these are typically "under the hood" methods implemented for you in the framework, you can override them, and there are plenty of uses for class methods in application code as well.

    Unlike C++, though, there is no storage block for class methods. If you need to have class methods store data, you have to use global variables.
  • davo666davo666 Posts: 65Registered Users
    edited March 2009
    thanks :) im learning !

    now (because these are C callback functions i cannot change the arguments :( )

    is there a way for an_object which the link you posted alluded to to be declared as 'extern' so that the pure C can see it without it being passed as an argument!

    thanks!
  • PhoneyDeveloperPhoneyDeveloper Posts: 1,431Registered Users
    edited March 2009
    You just need to architect this correctly in layers.

    You can have functions (not methods) in .m files that have C linkage but which contain Objective-C code. These functions are declared in standard .h files and can be called from C code in .c files. These .m files can have file scope statics that are Objective-C objects.

    So you can have a structure like this

    1)Objective-C -> 2)pure C -> 3)Objective-C functions with C linkage -> 1)Objective C.

    So in your example you could have that alert code in your appDelegate and your layer 3 would call the method in your appDelegate that shows the alert. App Delegate is just an example. In fact it could be any object in your app and it would make sense to have a separate singleton object that handled all the callbacks from the C code.
Sign In or Register to comment.