Alex Rivera | Logout

How to create global functions in Objective-C

Asked 2011-09-03T16:00:55.577
35

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.h likes this

#include <Foundation/Foundation.h>

- (void)printTest;

and in the functions.m

#import "functions.h"

- (void)prinTest {
    NSLog(@"test");
}

but it doesn't work. Says me: "Method definition not in a @implementation context".

Edit
Report

1 Answer

0

What your error is referring to is you need to have that method in an interface.

@interface SomeClass
- (void)printTest;
@end

To use a static void throughout your app (where you have included your Functions.h), try the following:

void printTest ()
{
    /* do your print stuff */
}
answered 2011-09-03T16:24:38.100

Your Answer