Standard Swift strings use double quotes
, but you can’t include line breaks in there.
If you want multi-line strings you need slightly different syntax: start and end with three double quote
marks, like this:
var str1 = """
This goes
over multiple
lines
"""
Swift is very particular about how you write those quote marks: the opening
and closing
triple must be on their own line
, but opening and closing line breaks won’t be included in your
final string.
If you only want multi-line strings to format your code neatly, and you don’t want those line breaks to actually be in your string, end each line with a \
, like this:
var str1 = """
This goes \
over multiple \
lines
"""
You can write \n
in any string to add a line break inside your text.
This code creates multi-line strings correctly - true or false?
01
var henley = """I am the master
of my fate
I am the captain of my soul"""
02
var eliot = "This is the way the world ends
Not with a bang but with a whimper"
03
var joseph = """
When I am an old woman,
I shall wear purple
with a red hat that doesn't go,
and doesn't suit me
"""
04
var coleridge = """
Water, water, everywhere
and not a drop to drink"""
Answer:
01 - false
The final three quotes
must be on a line by themselves.
02 - false
Multi-line strings must start and end with three double quotes
on lines by themselves.
03 - true
04 - false
The final three quotes
must be on a line by themselves.
欢迎体验我的作品之一:小笔记-XNote
笔记一步到位!
App Store
搜索即可~