Swift Quick Tips

Multi-Line String Literals

Posted by Sasha Prokhorenko on June 2, 2017

Only a few days left before WWDC 2017. I hope that we’ll see a lot of great and useful stuff, besides Swift 4, from Apple which can help us to make the world a better place.

One of the exciting features of upcoming Swift 4 is Multi-Line String Literals. I often need a quick way to create a prototype app with JSON data. Usually, I need to save data to local JSON file and then read from file or use network layer. That takes time. In Swift 4 we can use [Multi-Line String Literals](https://github.com/apple/swift-evolution/blob/master/proposals/0168-multi-line-string-literals.md. 

Example:  I used http://beta.json-generator.com to generate fake JSON data for this example. The result JSON looks like: JSON sample.json

Previously it wasn’t possible to use such strings like string literals. In Swift 4 you can just copy/past it like this:

""" JSON/XML/OTHERS COMPLEX STRINGS """ Multi-Line String Literals.playground Multi-Line String Literals.playground