Free preview lesson
Meet Swift and Your First Values
Say Hello to Swift in a Playground
Why this matters: before you can build an app, you need a safe place to try out code and see what it does. A playground is a scratch area in Xcode where Swift code runs right away and shows you the result. Swift is the programming language Apple uses to build iPhone and Mac apps, so every line you learn here will later appear in your own app.
To begin, create a new Swift Playground in Xcode by choosing File > New > Playground. Use a playground, not your app project, for these practice steps, because an ordinary file in an app project will not run a loose line of code like this. Look for an empty page where you can type. Anything you write there runs as soon as the playground updates.
Try this simple task:
- Delete any sample code in the playground so you start with a blank page.
- Type the line print("Hello, my to-do app!") exactly as shown, including the brackets and quotation marks.
- Look at the output area on the right side of the playground.
- Change the words inside the quotation marks and see the new message appear.
The word print is a command that shows text in the output. The words inside the quotation marks are called a string, which is simply text. Swift is strict about punctuation, so a missing bracket or quote mark will cause an error. If that happens, read the message, check your punctuation, and try again. Making mistakes here is normal and helps you learn.
What to do now: open a playground, type your own greeting using print, and run it. Try printing two different messages on separate lines.