I\'m developing an iphone app and I need to have some functions to use globally in my classes.
But how can I do this?
I just tried to create functions.
Two options here. First is create a class method in a static class:
Header:
#import
@interface GlobalStuff : NSObject {}
+ (void)printTest;
@end
Implementation:
#import "functions.h"
@implementation GlobalStuff
+ (void) printTest {
NSLog(@"test");
}
Call using:
#import "functions.h"
...
[GlobalStuff printTest];
The other option is to declare a global function instead of class:
Header:
void GSPrintTest();
Implementation:
#import
#import "functions.h"
void GSPrintTest() {
NSLog(@"test");
}
Call using:
#import "functions.h"
...
GSPrintTest();
A third (bad, but possible) option would be adding a category to NSObject for your methods:
Header:
#import
@interface NSObject(GlobalStuff)
- (void) printTest;
@end
Implementation:
#import "functions.h"
@implementation NSObject(GlobalStuff)
- (void) printTest {
NSLog(@"test");
}
@end
Call using:
#import "functions.h"
...
[self printTest];