Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 03-14-2009, 05:43 AM   #1 (permalink)
New Member
 
Join Date: Jan 2009
Posts: 65
davo666 is on a distinguished road
Default Mixing pure C and Objective C

hello,

in a C function i have these lines:

Code:
	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
davo666 is offline   Reply With Quote
Old 03-14-2009, 09:37 AM   #2 (permalink)
Registered Member
 
Join Date: Nov 2008
Posts: 253
zhyla is on a distinguished road
Default

I don't see self in the code listing, so I'm confused by your error. Sure you got the right lines?
zhyla is offline   Reply With Quote
Old 03-14-2009, 11:04 AM   #3 (permalink)
Registered Member
 
RickMaddy's Avatar
 
Join Date: Oct 2008
Location: Denver, CO
Posts: 2,121
RickMaddy will become famous soon enough
Default

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.
RickMaddy is offline   Reply With Quote
Old 03-14-2009, 11:34 AM   #4 (permalink)
Registered Member
 
Join Date: Jan 2009
Location: San Diego, CA
Posts: 406
jtara is on a distinguished road
Default

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.

Last edited by jtara; 03-14-2009 at 11:37 AM.
jtara is offline   Reply With Quote
Old 03-14-2009, 04:42 PM   #5 (permalink)
New Member
 
Join Date: Jan 2009
Posts: 65
davo666 is on a distinguished road
Default

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
davo666 is offline   Reply With Quote
Old 03-14-2009, 05:04 PM   #6 (permalink)
New Member
 
Join Date: Jan 2009
Posts: 65
davo666 is on a distinguished road
Default

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...
davo666 is offline   Reply With Quote
Old 03-14-2009, 05:15 PM   #7 (permalink)
Registered Member
 
Join Date: Jan 2009
Location: San Diego, CA
Posts: 406
jtara is on a distinguished road
Default

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.
jtara is offline   Reply With Quote
Old 03-14-2009, 05:21 PM   #8 (permalink)
New Member
 
Join Date: Jan 2009
Posts: 65
davo666 is on a distinguished road
Default

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!
davo666 is offline   Reply With Quote
Old 03-15-2009, 03:16 PM   #9 (permalink)
New Member
 
Join Date: Sep 2008
Posts: 1,431
PhoneyDeveloper is on a distinguished road
Default

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.
PhoneyDeveloper is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 332
3 members and 329 guests
guusleijsten, Kryckter, LEARN2MAKE
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,649
Threads: 94,113
Posts: 402,880
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Anwerbl
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 08:47 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0