Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift protocol for string interpolation

What protocol do I have to implement to control the way an object is represented within a string interpolation in Swift?

I wan't to specify what get's printed in something like this:

struct A{

}

var a = A()
println("\(a)")
like image 826
cfischer Avatar asked Sep 12 '14 20:09

cfischer


People also ask

How do I use string interpolation in Swift?

To use string interpolation inside a string that uses extended delimiters, match the number of number signs after the backslash to the number of number signs at the beginning and end of the string. For example: print(#"6 times 7 is \#(6 * 7)."#) // Prints "6 times 7 is 42."

What is string interpolation method?

String interpolation is a technique that enables you to insert expression values into literal strings. It is also known as variable substitution, variable interpolation, or variable expansion. It is a process of evaluating string literals containing one or more placeholders that get replaced by corresponding values.

What is CustomStringConvertible?

A type with a customized textual representation. Types that conform to the CustomStringConvertible protocol can provide their own representation to be used when converting an instance to a string.

What is string describing Swift?

A string is a series of characters, such as "Swift" , that forms a collection. Strings in Swift are Unicode correct and locale insensitive, and are designed to be efficient. The String type bridges with the Objective-C class NSString and offers interoperability with C functions that works with strings.


2 Answers

You need to implement the Printable protocol:

This protocol should be adopted by types that wish to customize their textual representation. This textual representation is used when objects are written to an OutputStreamType.

protocol Printable {
    var description: String { get }
}

There's also the DebugPrintable protocol when it's only for debugging purposes:

This protocol should be adopted by types that wish to customize their textual representation used for debugging purposes. This textual representation is used when objects are written to an OutputStreamType.

protocol DebugPrintable {
    var debugDescription: String { get }
}

Documentation (Thanks @MartinR)

Note: As @Antonio and @MartinR mentioned in the comments, this doesn't work in the playground (as of Xcode6 GM anyway); that's a known bug. It does work in compiled apps.

From the Xcode6 GM Release Notes:

In Playgrounds, println() ignores the Printable conformance of user-defined types. (16562388)

As of Swift 2.0 Printable has now become CustomStringConvertible. Everything stays the same as before, you still need to implement

 var description: String { get }

But now its called CustomStringConvertible. And debug is CustomDebugStringConvertible

like image 123
Mike S Avatar answered Sep 28 '22 09:09

Mike S


In Swift 5 Apple introduced Custom String Interpolation.

Suppose you have person struct with two properties name and age.

struct Person {
  var name: String
  var age: Int
} 

If you wanted to add a special string interpolation for that so that we can print persons in descriptive way, we can add an extension to String.StringInterpolation with a new appendInterpolation() method.

 extension String.StringInterpolation {
       mutating func appendInterpolation(_ person: Person) {
       appendInterpolation("My name is \(person.name) and I'm \(person.age) years old.")
    }
}

Now If we print the person details like:

 let person = Person(name: "Yogendra", age: 28)
 print("Person Details: \(person)")

Output will be:

Person Details: My name is Yogendra and I'm 28 years old.
like image 36
Yogendra Singh Avatar answered Sep 28 '22 07:09

Yogendra Singh