2

I have a global enum I use for global functions that I only want to run when the app is in debug. It looks something like this:

public enum Debug {
    static func print(_ items: Any..., separator: String = " ", terminator: String = "\n") {
        #if DEBUG
        Swift.print(items, separator: separator, terminator: terminator)
        #endif
    }
}

However, when I use

Debug.print(35)

the output is

["35"]

What I want is for the output to look just like a regular print statement:

35

Anyone know what I'm doing wrong or could do different?

It kind of looks like I have to "unpack" the items parameter and put each one in the print statement individually, but that seems like the wrong approach.

public enum Debug {
    static func print(_ items: Any..., separator: String = " ", terminator: String = "\n") {
        #if DEBUG
        for item in items {
            Swift.print(item, terminator: separator)
        }
        Swift.print("", terminator: terminator)
        #endif
    }
}

This works...but makes me cringe. There's got to be a better solution...

A. L. Strine
  • 611
  • 1
  • 7
  • 23

1 Answers1

3

You can simply map your items into strings, join them with the separator and print the resulting string:

public enum Debug {
    static func print(_ items: Any..., separator: String = " ", terminator: String = "\n") {
        #if DEBUG
        Swift.print(items.map({String(describing: $0)}).joined(separator: separator), terminator: terminator)
        #endif
    }
}

Or simply:

Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
  • That's a good answer. I was hoping to avoid a loop - see my updated question – A. L. Strine Jul 15 '20 at 23:01
  • I might be wrong but I don't think you have an alternative – Leo Dabus Jul 15 '20 at 23:04
  • If in 24 hrs nobody has an alternative, I'll mark this as the answer. I think you're right, I just don't want you to be, haha. – A. L. Strine Jul 15 '20 at 23:07
  • Smart! My guess is the map function is faster than a for loop, so I'll accept this as a reasonable compromise – A. L. Strine Jul 15 '20 at 23:45
  • It is not about speed. It is about achieving the same result as if you were actually printing them – Leo Dabus Jul 15 '20 at 23:46
  • Really? I thought that the for loop was slower than other iterating functions because of what happens in the back end, so I was trying to avoid having it run every time I print something out. Either way, this is basically what I was looking to do, so I'll keep the accepted status. – A. L. Strine Jul 15 '20 at 23:48
  • 1
    My intent was to replicate the behavior you would expect when using the original print function – Leo Dabus Jul 15 '20 at 23:50
  • I see what you mean now. Well, it works! So thank you! – A. L. Strine Jul 15 '20 at 23:50
  • 1
    From what I understand, it just gives --more-- information. It doesn't do an `#if DEBUG` check. Also, I want the option to print to a log file instead when this goes out to production so I have something to reference when errors occur on devices out of the office. – A. L. Strine Jul 16 '20 at 00:01