Textual Representation for Classes (Swift)

Swift make it easy to convert a custom class to a string (e.g. for use with print) via the CustomStringConvertible protocol. (a clear improvement over earlier implementations)

class X : CustomStringConvertibleĀ  {
  var description: String{
    return "This is class X"
  }
}

print(X()) //output is: "This is class X"

One thought on “Textual Representation for Classes (Swift)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s