Pretty Print, please
Tags: ios, iphone, swift
Published: 2022-04-03 17:18:17

<p class="subtitle">
When doing some light debugging most of us tend reach for the good old <em>print()</em> statement right? Yeah, it's just me 😉
</p>

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f2jbpv61wonw7scpz2wj.png) 

You might add some **&&&** or **===** to make your print **REALLY** stand out from all that output, but after a while it gets really hard to find anything in there. That is where this helper method comes in!

And also, **EMOJIS!** 👍

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1z4ewk2vy8uqtghgh64u.png) 

---

I found this awesome post by [Andyy Hope][andy] where he really goes into detail how everything works, so if you are interested I recommend going over that whole post. It has 2 parts, and is WELL worth a read! 🤘

For all of you anxious to try it out right away, behold this helper method. ⬇️

---

Create a new Swift file in your project, and paste this code:

```swift
import Foundation

enum log {
    case ln(_: String)
    case obj(_: String, _: Any)
    case error(_: Error)
    case url(_: String)
    case any(_: Any)
    case date(_: NSDate)
}

postfix operator /

postfix func / (target: log?) {
    guard let target = target else { return }
    
    func log<T>(_ emoji: String, _ string: String = "", _ object: T) {
        #if DEBUG
        print(emoji + "\(string)→" + " " + "\(object)")
        #endif
    }
    
    switch target {
    case .ln(let line):
        log("✏️", "", line)
        
    case .obj(let string, let obj):
        log("📦", string, obj)
        
    case .error(let error):
        log("❗️❗️❗️", "", error)
        
    case .url(let url):
        log("🔗", "", url)
        
    case .any(let any):
        log("⚪️", "", any)
        
    case .date(let date):
        log("⏰", "", date)
    }
}
```

---

To make use of that "DEBUG" flag, find the following:

* Go to your **Project Settings**
* Open **Build Settings**
* In the search, type **"Flag"**
* Open **Swift Compiler - Custom Flags** and **Active Compilation Conditions**
* For **Debug** insert a value -> "DEBUG"

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q5nmc7strrkk0t3yx6mm.png) 

---

And now for the fun part! Anywhere in your code use this

```swift
log.ln("So long PRINT! Been nice knowin' ya!")/
```
That prints: ✏️→ **So long PRINT! Been nice knowin' ya!**

---

<p class="info">
<strong>!Important</strong> that little <em>/</em> at the very end is very important. That is a <strong>postfix operator</strong> and without it the log won't print out.
</p>

Andyy goes over that in detail in his post so head on [there][andy] if you are interested how all that works.
You can even use your own character but Andyy recommended it and who am I to doubt Andyy 😅

---

## Other available logs

<div>
<table style="width:100%; background-color: #1d2031; border-radius: 12px; padding: 40px;">
<tr>
<th style="color: var(--pink);">Input</th>
<th style="color: var(--pink);">Output</th>
</tr>
<tr>
<td><pre>log.ln("Text")/</pre></td>
<td style="color: white;">✏️→ Text</td>
</tr>
<tr>
<td><pre>log.obj("My Object", Object)/</pre></td>
<td style="color: white;">📦  My Object → Object Properties</td>
</tr>
<tr>
<td><pre>log.error(error)/</pre></td>
<td style="color: white;">❗️❗️❗️→ Error Properties</td>
</tr>
<tr>
<td><pre>log.url("undeadpixel.dev")/</pre></td>
<td style="color: white;">🔗 → undeadpixel.dev</td>
</tr>
<tr>
<td><pre>log.date(Date())/</pre></td>
<td style="color: white;">⏰ → 2020-04-25 17:45:14 +0000</td>
</tr>
<tr>
<td><pre>log.any("Whatever you want")/</pre></td>
<td style="color: white;">⚪️ → Whatever You want</td>
</tr>
</table>
</div>

---

It is also quite easy to spot the **logs** that you added. Sometimes when I want to search over the project for a specific **print** and I type *"print("* in the searchBar, all of the prints from every file are there. Now number of results is far narrower so my own code is easier to spot! 😳

**That's it!** Feel free to modify the helper and add your own emoticons or customise it to your hearts content!

---

Thank you for reading, you superb person! ♥️

Get in touch on [Twitter][twitter]

Cheers! 🍻

[andy]: https://medium.com/swift-programming/swift-prettify-your-print-statements-pt-1-64832bb7fafa
[undead]: https://undeadpixel.dev
[twitter]: https://twitter.com/emin_ui