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?
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.
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.
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.
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.
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.
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.
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!