How to create extension for print()

HI CodeCrew,

This seems to be very easy but couldn’t find an answer anywhere in the web.

Basically what I want to do is replace or rename the function print() to something like DBGPrint()… In C, if I write the DBGPrint() it would look something like below. I want to make a similar function in swiftui.

DBGPrint() {
    #ifdef APP_DEBUG

I think the cleanest way to do this is to create extension on the print()… I just can’t find anything on the web how to do this.


extenstion ???? {
   func  DBGPrint(....) {
        #ifdef APP_ERR_DBG
        #else #ifdef APP_ERR_WARN_DBG

The app will have different #def APP_ERR_???

The reason for this:

  • control what is being print. e.g. if I just want to see print() that is mark with error, then I’ll just define APP_ERR_DBG
  • should I release the app, I’ll turn off all the DBG #def and the dbgprint() won’t be compiled… I don’t have to remove the dbgprint() commands… Can you confirm if I can do this in swiftui?


You’re close, in how to do this, but the reason you can’t find anything is because this isn’t exactly how it works. You don’t ever extend print because it’s a function, and you can’t extend functions.

Side note, this all is not related to SwiftUI, this is all about Swift the programming language itself. SwiftUI is a framework for the UI of the app.

I see the point in this, but honestly over time I would definitely remove these statements because your console is going to eventually be filled with a bunch of print statements and it will be hard to tell what’s actually relevant, and later this is where using Breakpoints is super useful too.

How to do this

Soo the best way to actually do this would be to make some kind of structure or global function.
You could create an enum for types of messages, and a struct with a static function for making different kinds of prints, passing in the enum to represent what kind of print statement it is.

I’m not completely sure about wrapping the whole thing in an #if DEBUG if that would really make it not compile for release. Printing can add a tiny tiny bit of slow down, but you’d have to litter your code with thousands for it to really matter, but that goes back to my point that you should remove print statements and not just let them pile up in your codebase.

enum MessageType: String {
  case info = "ℹ️"
  case warning = "⚠️"
  case error = "❌"
  case debug = "🔨"
struct FancyPrint {
    static func print(type: MessageType, message: String) {
        print("\(type) \(message)")

You would then use it like:

FancyPrint.print(type: .info, message: "This is an info message")
FancyPrint.print(type: .error, message: "This is an error message")
1 Like

Thanks Mikael…

Need to find out if #def is possible in swift… If print()s are compiled on release, then definitely a no no…

I’m used to console debugs that I prefer them over break points… Its does pile up though as you said but really helpful to look at the flow. I guess only few likes going through bunch of debugs…

Yea the static functions will work but that will be compiled on release…

Anyways… Will research on the #ifdef… Otherwise will cleanup the prints and use break points…


You can use #if DEBUG, but I’m not sure how that would work with a whole structure and using that structure

Great… Thank again Mikael…

1 Like