Swift 4 - Multiline string literals support

Multi-line support in Swift 4. Now add multi line support with double quotes using Swift 4 and Xcode 9. Tutorial with example on adding support.

Swift 4 introduced a concept of multiline text support for code. This allows developers to write descriptive comments or JSON payload using """. Unlike previous comment support this approach does handle complicated plain text in easier way.

I experienced it while writing tutorial for Codable protocol where I had to manually write several JSON text. Using such multiline support, I was able to just copy and paste JSON from online resources and use it directly. Plus, it also allows to directly manipulate text by directly calling Swift methods. Another advantage is that you can use double quotes without escaping. Triple double quotes take care of escaping quotes for you internally.

Too much rambling and no examples. I will just stop here and start providing some examples,

  • You can use """ for beginning and ending of multiline text. In between any text can be provided. For example,
let sample = """
            this is multiple support for us
            you can do it as it is
            something to fill it in
            """

This will print,

Screen-Shot-2017-10-29-at-11.22.42-PM

  • If you do not want newline as they are automatically inserted during string literals creation, you can end the line with trailing \. This will remove any new line at the end
let sample = """
             this is multiple support for us \
             you can do it as it is \
             something to fill it in
             """

This will print,

Screen-Shot-2017-10-29-at-11.28.21-PM

  • You can directly apply any string related method to multi-line string literal
let personJSONData = """
                     { "name": "tom" }
                     """.data(using: .utf8)

Please note how we applied data(using: .utf8) directly to the multiline string literal and personJSONData is of type Data which represents the original JSON payload

  • Multiline string literal is useful to declare and process JSON

For example,

let jsonData = """
                [
                {"first": "Tom", "last": "Smith", "age1": 31},
                {"first": "Bob", "last": "Smith", "age1": 28}
                ]
               """

Please note how JSON payload has been easily handled between three single quotes with Swift 4 support. This would've been difficult earlier without this support. Typing JSON in this form has the advantage that developer no longer have to escape quotes and perform complex formatting for long JSON payload examples.

  • Any string indentation must start at or after indentation of double quotes delimiter

This is a mandatory requirement. Any string literal starting at indentation before double quotes delimiter will result in compiler error.

For example, following multi-line string literal is valid,

let jsonData = """
                [
                {"first": "Tom", "last": "Smith", "age1": 31},
                {"first": "Bob", "last": "Smith", "age1": 28}
                ]
               """

Following is not,

let jsonData = """
                [
                {"first": "Tom", "last": "Smith", "age1": 31},
                {"first": "Bob", "last": "Smith", "age1": 28}
            ]
               """

and will fail to compile with the following error,

Insufficient indentation of line in multi-line string literal
  • Multi-line strings equality
    It is important to note that whether you add new line or not decides the multi-line strings equality.

For example consider two strings below,

let first = """
            this is multiple support for us
            you can do it as it is
            something to fill it in
            """

let second = """
             this is multiple support for us
             you can do it as it is
             something to fill it in
             """
if sample == sample1 {
    print("Strings are equal")
}

Above comparison will print Strings are equal at the end.

Now consider two strings below. Note that adding \ at the end of string literal will remove newline.

let first = """
            this is multiple support for us\
            you can do it as it is\
            something to fill it in
            """

let second = """
             this is multiple support for us
             you can do it as it is
             something to fill it in
             """
if sample == sample1 {
    print("Strings are equal")
} else {
    print("Strings are not equal")
}

Above comparison will print Strings are not equal at the end.

This is all I have for multi-line string support in Swift 4. Please feel free to let me know if I have missed anything or there are any discrepancies in the post. You can directly send me an email or Tweet to my Twitter handle @jayeshkawli. Thanks a lot for reading. Happy Coding!