Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove println() for release version iOS Swift

I would like to globally ignore all println() calls in my Swift code if I am not in a Debug build. I can't find any robust step by step instructions for this and would appreciate guidance. is there a way to do this globally, or do I need to surround every println() with #IF DEBUG/#ENDIF statements?

like image 706
Nate Birkholz Avatar asked Nov 13 '14 16:11

Nate Birkholz


1 Answers

The simplest way is to put your own global function in front of Swift's println:

func println(object: Any) {     Swift.println(object) } 

When it's time to stop logging, just comment out the body of that function:

func println(object: Any) {     // Swift.println(object) } 

Or you can make it automatic by using a conditional:

func println(object: Any) {     #if DEBUG         Swift.println(object)     #endif } 

EDIT In Swift 2.0 println is changed to print. Unfortunately it now has a variadic first parameter; this is cool, but it means you can't easily override it because Swift has no "splat" operator so you can't pass a variadic in code (it can only be created literally). But you can make a reduced version that works if, as will usually be the case, you are printing just one value:

func print(items: Any..., separator: String = " ", terminator: String = "\n") {     Swift.print(items[0], separator:separator, terminator: terminator) } 

In Swift 3, you need to suppress the external label of the first parameter:

func print(_ items: Any..., separator: String = " ", terminator: String = "\n") {     Swift.print(items[0], separator:separator, terminator: terminator) } 
like image 179
matt Avatar answered Oct 13 '22 04:10

matt