Explaining the role of CustomStringConvertible Protocol — Swift

Batikan Sosun
2 min readMar 2, 2020

--

Today I want to discuss the CustomStringConvertible protocol. CustomStringConvertible is a protocol that helps you to do great works.

Photo by Harley-Davidson on Unsplash

I loved the above image it’s so charming :) whatever, back to the topic of discussion

Now, an important point if you have an object that is such as struct or class to have a custom representation of the properties that you have declared.

Logs are important when you need to access an issue. And then the most important are these logs should be readable and understandable.

For example, we will create a class named animal and then print it.

If we print the created class the result is unreadable and not understandable for humans right?
The result is below as you see. This is undesirable.

Now, if we implement the CustomStringConvertible we will see the great result as we want.

According to Apple’s documentation, We need to create a description String which will be a textual representation of self.

The result is more readable and understandable.

And the result is desirable. We can modify the description string as we wish.

Thanks for reading :)

--

--

Batikan Sosun
Batikan Sosun

Written by Batikan Sosun

Tweeting tips and tricks about #swift #xcode #apple Twitter @batikansosun Weekly Swift Blogging

No responses yet