Swift Quick Tips: Multi-Line String Literals (Swift 4)

Photo by: Roman Bozhko, Los Angeles, United States. Source: https://unsplash.com

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.
Example:
I used http://beta.json-generator.com to generate fake JSON data for this example. The result JSON looks like:

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:

Multi-Line String Literals.playground

Software Engineer with a decade of experience in software engineering — https://minikin.me

Software Engineer with a decade of experience in software engineering — https://minikin.me