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 07-06-2010, 06:22 PM   #1 (permalink)
Registered Member
 
K2mia's Avatar
 
Join Date: Jun 2010
Posts: 29
K2mia is on a distinguished road
Default Ansi C + Objective-C ?

Everything I read seems to indicate that Ansi C is compatible with Objective-C - so I am wondering, if I have a 'Window' application created, is there a way for me to access variables and functions that I define in main.c ( the ansi C portion of the app ) from the Objective-C classes in the app?

For example suppose I have a function:

int getIntParam();

defined in main.m.

a) How would I access the main.m file from a ViewController class?
b) How would I access the getIntParam() function from within ViewController?

Any insight would be greatly appreciated!

-Andy
K2mia is offline   Reply With Quote
Old 07-06-2010, 06:33 PM   #2 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by K2mia View Post
Everything I read seems to indicate that Ansi C is compatible with Objective-C - so I am wondering, if I have a 'Window' application created, is there a way for me to access variables and functions that I define in main.c ( the ansi C portion of the app ) from the Objective-C classes in the app?

For example suppose I have a function:

int getIntParam();

defined in main.m.

a) How would I access the main.m file from a ViewController class?
b) How would I access the getIntParam() function from within ViewController?

Any insight would be greatly appreciated!

-Andy
Yes, Objective C is a clean superset of ANSI C, and you can freely mix ANSI C with Objective C code.

If you want variables from main visible in the rest of your program, you should define them in main.m, and define them as extern in main.h. Then #include main.h in your other files. That makes them application globals.

I haven't tried it, but it should work.

It's been a long time since I've done vanilla C. What is getIntParam()? Is that a standard library function to get the command line parameters for an app launched from the command line or something? Doing a little digging in my old C manual, and in Google, it looks like it's Java, not C.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 07-06-2010, 07:42 PM   #3 (permalink)
Registered Member
 
K2mia's Avatar
 
Join Date: Jun 2010
Posts: 29
K2mia is on a distinguished road
Default

Heya, thanks for the reply. The function I mentioned was just something I made up to illustrate a simple type of function I may want to be able to retrieve from a ViewController or some other ObjC construct. I will play around with this and hopefully its straightforward enough, ultimately I want to build some BSD socket control functions in main.m and be able to access them from the ViewControllers.

Thanks!
Andy
K2mia is offline   Reply With Quote
Old 07-06-2010, 08:20 PM   #4 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by K2mia View Post
Heya, thanks for the reply. The function I mentioned was just something I made up to illustrate a simple type of function I may want to be able to retrieve from a ViewController or some other ObjC construct. I will play around with this and hopefully its straightforward enough, ultimately I want to build some BSD socket control functions in main.m and be able to access them from the ViewControllers.

Thanks!
Andy
Andy,

By a pretty strong convention, main is completely vanilla, and has NO custom code in it. A Cocoa/Objective C programmer would not think to look in main for custom code. I would advise against putting any code in main at all.

Since Objective C is a pure superset of C, you can write C functions in the middle of an Objective C source file, and call them from Objective C.

If you want to write a module of global C functions that get called from all over your app, you can do that, and then just include that module's header in all your other files.

Typically the application delegate is the center of a Cocoa program. Thats the object that the system notifies that the app has been launched, and it is also a "singleton" object that other objects in your app can easily find and send messages to.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 07-06-2010, 08:59 PM   #5 (permalink)
Registered Member
 
Join Date: Jun 2009
Location: Ypsilanti, Michigan
Age: 63
Posts: 1,549
RLScott is on a distinguished road
Default

As Duncan C said, you can write plain C functions inside any ".m" file. But to make it clear that the C function, or global variable, or whatever, is not part of some class implementation, put all these things either before the "@implementation" statement or after the "@end" statement.

Robert Scott
Ypsilanti, Michigan
RLScott is offline   Reply With Quote
Old 07-07-2010, 08:12 AM   #6 (permalink)
Registered Member
 
K2mia's Avatar
 
Join Date: Jun 2010
Posts: 29
K2mia is on a distinguished road
Default

Ahh, now I am visualizing it, I had tried to incorporate those functions into the ViewController ealier but if I recall correctly I put them inside the @implementation block - now I see why it didn't work! Thank you guys for explaining this!
K2mia 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: 333
9 members and 324 guests
anothermine, Chickenrig, firecall, givensur, iNet, michaelhansen, Objective Zero, PixelInteractive, stanny
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,657
Threads: 94,118
Posts: 402,892
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jenniead38
Powered by vBadvanced CMPS v3.1.0

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