Step 1 of 5 · Reading · ~3 min
Learn
Getting Started
Hello, World
Most languages make you build a scaffold before you can print anything: a class, a main
function, an entry-point declaration. Swift skips all of it. A .swift file's top-level code
is the program — statements run top to bottom, in the order you wrote them.
print("Hello, World!")
print("Ship That Code")
//> Hello, World!
//> Ship That Code
Two statements, two lines. That is the entire program.
Why is there no main function?
There is one, technically — Swift generates it for you when a file contains top-level
statements. The rule that matters: at most one file in a program may have top-level code.
In a single-file exercise like this one that file is yours, so you never think about it. A real
iOS app instead marks a type with @main and Swift calls into that. Same idea, different door.
What print actually does
print writes its arguments to standard output and then adds a newline. Both halves are
adjustable.
| You write | You get |
|---|---|
print("a") | a followed by a newline |
print("a", "b") | a b — arguments joined by a space |
print("a", "b", separator: "-") | a-b and a newline |
print("a", terminator: "") | a with no newline |
The terminator: form is how you build one line out of several calls:
print("load", terminator: "")
print("ing...")
//> loading...
There is no line break between load and ing..., because the first call was told not to end
its line.
Statically typed — but you rarely say a type out loud
Swift checks every type at compile time, so a whole family of mistakes never survives to runtime. It also infers almost all of those types from context, which means you write far fewer of them than in Java or C#.
let greeting = "Hello"
let year = 2026
print(greeting, year)
//> Hello 2026
You never wrote String or Int; the compiler worked both out from the literals. It will also
reject greeting + year at compile time, before the program ever runs.
Getting the output exactly right
Automated tests compare your output character for character. Two near misses cost people a pass far more often than any real bug:
✗ Fails
print("Hello World") // no comma
print("hello, world!") // wrong capitals
✓ Passes
print("Hello, World!")
Punctuation, capital letters and spaces are all part of the answer.
Your exercise
Print exactly Hello, Swift! — comma after Hello, capital S, exclamation mark at the end.
The starter is a single comment; add one print call under it.
The mistake the grader catches here is the near miss. This lesson's examples all print
Hello, World!, and the reflex is to copy one of them — that fails, because the test wants
Hello, Swift!. So do Hello Swift! with no comma and hello, swift! in lower case. The test
compares the whole line, so the program can be perfectly correct Swift and still not pass.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…