Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cocoa - defining a global function like NSLog

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] ?

like image 380
Oliver Avatar asked Aug 14 '11 11:08

Oliver


1 Answers

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!

like image 175
Yuji Avatar answered Sep 27 '22 18:09

Yuji