5

See that post and the accepted answer :

In XCode, is there a way to disable the timestamps that appear in the debugger console when calling NSLog?

How may I, for that example or any other situation, declare a global function that include objective-c calls that I may be able to call directly, like NSLog, without having to make a call like [MyClass myFunction] ?

Community
  • 1
  • 1
Oliver
  • 23,072
  • 33
  • 138
  • 230
  • possible duplicate of [How can I write an function in objective-c, that I can use over any object in my iPhone app?](http://stackoverflow.com/questions/788706/how-can-i-write-an-function-in-objective-c-that-i-can-use-over-any-object-in-my) – miku Aug 14 '11 at 11:22

1 Answers1

6

Just use the standard C syntax; remember, Objective-C is a strict superset of C.

In a .h file, write the declaration

extern return_type function_name(argument_type1 argument_name1,argument_type2 argument_name 2);

and in a .m file (or .c file or whatever), write the implementation

return_type function_name(argument_type1 argument_name1,argument_type2 argument_name 2){
      ....
}

If it's in a .m file, it should be put outside of @implementation ... @end block. (Well, you can put your function within it, but I find it confusing.) That's it!

Yuji
  • 34,103
  • 3
  • 70
  • 88
  • I have this error at compile time : Undefined symbols for architecture i386: "_MyNSLog", referenced from: -[MyClass doJob:] in MyClass.o ld: symbol(s) not found for architecture i386 collect2: ld returned 1 exit status – Oliver Aug 14 '11 at 12:20
  • 1
    You forgot to include the `.m` file in your target, or your function implementation (in the `.m` file) has a name that’s different from the declaration in the `.h` file. – gcbrueckmann Aug 14 '11 at 12:24
  • Thank you for this. I had been struggling to get the right implementation of something like this for a bit. Got it all going now. – djneely Dec 04 '13 at 17:40