BeginnerLesson 1 of 16
Hello World, print & Strings – Swift
Your first Swift program, output, and string interpolation.
A Swift program is a text file with statements that run from top to bottom; a main function is not needed in scripts. You output something with print.
print("Hello World!")
print("I am learning", "Swift")
print("Number:", 42)You insert values into text with string interpolation \(...), and any code may appear inside it. separator and terminator control the format of print.
let name = "Ada"
let alter = 36
print("\(name) is \(alter) years old")
print("Next year: \(alter + 1)")
print("a", "b", "c", separator: "-") // a-b-c
print("no line break", terminator: "")
print(" – continuing on the same line")Comments start with // (one line) or sit between /* ... */; with /// you write documentation comments, which Xcode shows as Quick Help. Multiline strings sit between three quotation marks; the indentation of the closing line is removed:
let gedicht = """
Line 1
Line 2 with "quotation marks"
"""
print(gedicht)
print("Tab:\tText\nNew line")
print(#"Raw string: \n stays as is"#) // # makes the string "raw"TipSemicolons at the end of a line are not needed. Swift names are written in
camelCase (meinWert), and types in PascalCase (MeinTyp).WarningStrings must be in double quotes
"...". Single quotes '...' do not exist; a single character is also written as "x" (with the type annotation Character).