Add a function globally to use throughout an iPhone App
I want to add a simple function to a central place so that I ca开发者_开发百科n use it without having to include the header files over and over again. How do I accomplish it?
Consider the function
void foo() {
printf("Bar");
}
I tried adding this to a foo.h file and then including it in the prefix.pch file. But a compilation error occurs saying duplicate symbol.
You need to use inclusion guards, like this:
#ifndef FOO_H
#define FOO_H
void foo(); //declaration
#endif
And in Foo.m
:
void foo() {
printf("Bar");
}
Or, even easier, you can simply use the #import
directive:
#import "Foo.h"
If you did all of the above, then the only way this could've happened is if you explicitly declared a new function with the same name inside another file
header (foo.h):
void foo();
implementation file (foo.m)
void foo() {
// blah
}
Stick your foo.h in your PCH and away you go.
Include your function in a separate class.
And include the header of that class to all of your classes.
精彩评论