Outputting with print() - Images: Japanese
Hey there, everyone!
In the previous article, we got Xcode and our Swift environment all set up — so this time, let's jump right in and write some actual code!
Writing code is sometimes called coding — you'll hear both terms used interchangeably.
Either programming or coding works just fine, so feel free to use whichever you prefer.
You might hear a colleague or senior developer say "can you handle the coding on this?" rather than "the programming" — so it's a handy word to have in your vocabulary.
Before we start coding, there's a bit of setup to do. Open up Xcode and take a look at the bottom-left area of the editor. It's the spot marked by the red arrow below.

You'll see a small triangle inside a box. If the triangle is pointing upward, go ahead and click it. You should then see something like this — pay attention to the area around the red arrow.

A new panel has appeared at the bottom! This is called the Debug Area (or debug console), and we're going to be using it constantly going forward. While you're still getting used to things, try to keep this panel visible at all times.
Now let's take a look at the editor. You should see some text already written there by default — something like this:
//: Playground - noun: a place where people can play import UIKit var str = "Hello, playground"
(This might look slightly different depending on your version or environment.)
Once you've confirmed it's there, go ahead and delete all of it. Don't worry about any of it — just wipe it clean.
You should now have an empty editor, like this:
Yep, just blank. Nothing there at all.
Now, type in the following. You could copy and paste it, but typing it yourself is better practice — so it's worth the extra few seconds.
print("Hello world")
One important note: make sure everything is typed in half-width (ASCII) characters.
Programming is done entirely with half-width characters. Some languages technically allow full-width or non-ASCII characters, but that's very much a niche use case — so stick to standard ASCII.
Once you've typed it in, wait a moment, then check the Debug Area we opened earlier. You should see this output:
Hello world
If it worked — congratulations! You've just written your first Hello world program in Swift. If it didn't work, double-check that everything was typed in half-width ASCII characters. At this stage, that's usually the only thing that trips people up.
The Hello world program is quite possibly the most famous program in the world — it does nothing more than print "Hello world".
Virtually every introductory programming book starts with a sample that prints "Hello world", "Hello Swift", "Hello, playground", or something similar.
Why did such a simple program become so legendary? The story traces back to Dennis Ritchie (Dennis MacAlistair Ritchie) — the developer of the C language and the UNIX operating system.
The introductory book on C that he authored opens with a sample that prints "hello, world" — and from there, the tradition of starting with a Hello world example spread across programming books and tutorials of all kinds.
Dennis Ritchie is the creator of C, the language that forms the foundation of most modern programming languages, and also the creator of UNIX, the OS that became the foundation of virtually every operating system in use today.
In other words, without Dennis Ritchie, the computing and internet world as we know it would not exist. He may well be the person who changed the world more than anyone else in the last several decades.
If you asked the author "who has helped you most in life?", the answer would be "Dennis Ritchie, without question."
He passed away in the autumn of 2011 — and it's a shame that his passing received so little coverage outside the tech world.
Now, let's talk about print() — the thing we just used. It's a function that outputs whatever you put inside the () to the Debug Area.
We put "Hello world" inside the (), so "Hello world" showed up in the Debug Area. Simple as that.
Try replacing the contents with 1, like this:
print(1)
And the output will be:
1
Works exactly as you'd expect. The key thing to take away: print() outputs whatever is inside the parentheses. Make sure that's locked in before moving on.
In the next article, we'll take a look at comments. See you there!
This article was written by Sakurama.
Author's beloved small mammal |
桜舞 春人 Sakurama HarutoA Tokyo-based programmer who has been creating various content since the ISDN era, with a bit of concern about his hair. A true long sleeper who generally feels unwell without at least 10 hours of sleep. His dream is to live a life where he can sleep as much as he wants. Loves games, sports, and music. Please share some hair with him. |
If you find any errors or copyright issues, please contact us.