3

prior to Swift 2 you could hide all your println() for release by having a little helper like this

func println(object: Any) {

#if DEBUG
    Swift.println(object)
#endif

If you change this to

func print(object: Any) {

#if DEBUG
    Swift.print(object)
#endif

it works as expected however print() has a new appendLine feature. So in your code you could write

println("Test", appendNewLine: false)

Doing this however means the above mentioned helper will not work anymore. Any suggestions of how to fix this. Thank you very much.

crashoverride777
  • 10,581
  • 2
  • 32
  • 56

3 Answers3

5

Use Swift.debugPrint() instead to turn off all the prints in release mode.

Qbyte
  • 12,753
  • 4
  • 41
  • 57
  • Thank you. It actually turns out that appendNewLine is only needed when you dont want to use it. So just saying print("Test") is exactly the same as println("Test"). You just need to say appendNewLine:false if you want to output into 1 line in the console. http://stackoverflow.com/questions/24072775/difference-between-println-and-print-in-swift?rq=1 – crashoverride777 Sep 07 '15 at 13:23
  • 3
    This is not correct as far as I can tell. Can you point to documentation about this, because it's not mentioned in the description of the function, and all I can find when searching for it is StackOverflow answers. It's possible there's some confusion caused by this issue: http://stackoverflow.com/questions/31218200/swift-println-doesnt-show-anything-into-view-device-logs-in-devices-tab – Jason Newell Feb 20 '16 at 12:56
  • 1
    According to this article the Swift compiler does not remove debugPrint at compile time: https://medium.com/ios-os-x-development/swift-log-devil-or-why-println-is-dangerous-46390453353d#.g28obzhp5 – wuf810 Jul 08 '16 at 10:14
5

For swift 2.2, here's what I use:

// Disable print for production.
func print(items: Any..., separator: String = " ", terminator: String = "\n") {
    #if DEBUG
    Swift.print(items[0], separator:separator, terminator: terminator)
    #endif
}
crashoverride777
  • 10,581
  • 2
  • 32
  • 56
huwiler
  • 915
  • 9
  • 9
  • 1
    Hey thanks, this is what I am doing as well at the moment. I edited your answer to include the line in the function, as that has changed as well. I marked it as correct also as this is the best solution at the moment and should help future readers. – crashoverride777 Aug 31 '16 at 16:11
3

Qbyte is on the right track, but debugPrint will print regardless of whether "DEBUG" is defined or not.

Why not change the helper to take "appendNewLine"? That is:

func print(object: Any) {

#if DEBUG
    Swift.print(object, appendNewLine: true)
#endif

or perhaps change the name to be a little more clear:

func printDuringDebug(object: Any) {

    #if DEBUG
        print(object, appendNewLine: true)
    #endif
Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
  • Thanks to you too. Thats actually what I tried too but had some issues with it. Will try again in 2 days when the GM is released. See my comment above tho, apparently you dont even need to write appendNewLine: true. I misunderstood that bit. – crashoverride777 Sep 07 '15 at 13:28