image source: pixel.com
Console logs are one of the most important parts of any application. Back in the older days of ObjC, we (developers) didn’t have the tools of beautifying the console logs. However, we could control the verbosity of logs when it comes to extensive debugging. Today, we have some additional features with swift which enable us to do magical stuff with console logs 🎩
Before learning the trick, let’s take an example of default console printing of an object.
Dev Tip: Fire up your playground and see yourself 💻
struct User {let firstName: Stringlet lastName: Stringlet age: Int}
let user = User(firstName: "John", lastName: "Doe", age: 22)print(user)
The example is pretty straightforward of a value type object. Hoping you already know the output:
It’s quite obvious that console is giving the information about an object, along with its properties and values (inside the brackets). Instead of the old style of console logging in ObjC, where it used to print the memory reference along with some properties of the object. It’s the time to reshape it 🏋️
Before going into the nitty-gritty of pretty printing, let’s talk about a swift protocol which gives us the power to actually make it happen. CustomStringConvertible is a protocol (blueprint) of converting any instance into a textual representation. Any type conforming to this protocol will leverage its power of custom string representation. Let’s see what’s in there for us.
The protocol comes with a requirement, so you need to implement it 👨🚀
public var description: String { get }
It’s a simple gettable string property that a conforming type will provide, in order use it. Let’s see what happens to User type when it conforms to the protocol:
extension User: CustomStringConvertible {var description: String {return """UserfirstName: \(firstName)lastName: \(lastName)age: \(age)"""}}
Here, we are trying to translate User to a meaningful printable object using Swift4 multi-line string literal. Hit save ⌨️
That looks 🆒 . Can’t resist adding emojis? Go ahead and add one 😉
Wohoo 🎉 Well, you have then open field to play the way you like it.
Motive; We ain’t gonna repeat this for every other type we have in our project. Are we? 🤔
Swift is a boon 👨🔬 It gives you more power than you could imagine. Ever thought of writing your own printable protocol? Let’s dive into it 🏊
Using swift’s protocol inheritance capabilities we are able to create our own printable protocol, which iterates through the class properties to print their respective values. So, instead of conforming to CustomStringConvertible and rewrite the printing, we conform to CustomPrintable protocol to get the desired logs. Awesome isn’t it? 🚀
In the end, adding little sweetness to that (of course emoji 😄) to make it pretty.
This can surely improve your dev experience as we all love console logs at all scales. Last but not least, enable your console only for DEBUG mode.
Feedback,👏 and stay tuned for more swifty tricks.
Say hello on twitter @khfarooq.
Thanks for reading! 🌟