index rss mastodon twitter github linkedin email
Álvaro Ramírez
sponsor

Álvaro Ramírez

23 November 2018 Multiline Swift strings

Paul Hudson's tweet introduced me to Swift's multiline string indentation control using closing quotes. Neat!

Being an org-mode fan, I thought I'd give Swift multiline strings a try using Org Babel's ob-swift. I get to verify it and document at the same time. Win.

Swift org mode source blocks (ie. BEGIN_SRC/END_SRC) can be added as follows:

#+BEGIN_SRC swift :exports both
  print("""
       Hello World
  """)

  print("""
       Hello World
       """)
#+END_SRC

#+RESULTS:
:      Hello World
: Hello World

By pressing C-c C-c anywhere in the code block, the snippet is executed and its output captured in the RESULT block. Super handy for quickly trying out snippets and keeping as future reference.

As a bonus, the above blocks can be exported to HTML (amongst other formats). With some styling, it looks as follows:

print("""
     Hello World
""")

print("""
     Hello World
     """)
     Hello World
Hello World