Cool Swift Tricks 2: No Escape

This is the second of four posts about random little Swiftisms for you to amaze your friends and confound your enemies. All of these came up in my real code recently, and they really did surprise one or more of my co-workers. The Swift language has a lot of cool features hidden away in its nooks and crannies, but most real developers are too busy getting real work done to keep up with all of them; so every once in a while I’ve submitted a pull request that made someone say, “Hey, I didn’t know about that!” If you already knew about these features, you can feel smug, and if you didn’t, now you do, so you can feel smug anyway.

Our tests translate Cucumber .feature files into actual XCTest cases. This means that every line of a .feature file must be effectively copied and pasted into the test code as a literal string.

So, for example, suppose we have a .feature file line like this:

Then the upload should be terminated

That ends up as Cucumberish code like this:

Then("the upload should be terminated") { _,_ in
    // test code goes here

That transformation can be largely automated without much difficulty (I won’t go into details here).
However, some of the .feature file lines contain double quotes:

Given Cathy is attempting to upload "Doc123"
When the app fails to upload "Doc123"

Transforming lines like that into literal strings is going to be a pain in the butt, because the double quotes will have to be escaped within the literal string:

Given("Cathy is attempting to upload \"Doc123\"") { _,_ in

That’s ugly to generate and ugly to read. But wait! Swift has another kind of literal string — one where you don’t need to escape double quotes internally:

Given(###"Cathy is attempting to upload "Doc123""###) { _,_ in

That’s called a raw literal. Not only does a raw literal string not require double quotes to be escaped (because the extra hash marks make it obvious to the compiler where the literal string’s delimited boundaries are), but also it jumps out of the screen at you, making the original .feature file line easy to see.

So I’ve translated all my .feature file lines into that kind of literal string. If the line contains double quotes internally, they don’t have to be escaped, and if the line doesn’t contain any double quotes, that doesn’t matter.

Actually, the triple hash signs are unnecessary. These strings would work just as well delimited by single hash signs:

Given(#"Cathy is attempting to upload "Doc123""#) { _,_ in

So why did I elect to use triple hash signs? Because they look cooler! And because they stand out more clearly. Even better, they elicited a gasp from a colleague, who didn’t know about raw literals.

Check out Cool Swift Tricks 1: Mirror, Mirror

Check out Cool Swift Tricks 3: The Garden of Forking Key Paths

You Might Also Like…

Swift 5.5: Replacing GCD With Async/Await

Multithreading! The mere word sends shivers up one’s spine. And if it doesn’t, it should. Main thread and background threads. Code that runs asynchronously. Code that can run simultaneously with other code. Code that can run simultaneously with itself. Code that can share data across threads — possibly with disastrous consequences. Concurrency. Multithreaded code is …

Swift 5.5: Replacing GCD With Async/Await Read More »

    Sign Up

    Get more articles and exclusive content that takes your iOS skills to the next level.