Unit 5: First App Flashcards
The Good Old Days
In this playground, you’ll participate in a special tradition that new programmers have enjoyed since the 1970s. But before you get there, you’ll need to learn a few things. It starts with how to configure your programming environment in Xcode.
As you’ve gone through several projects, you might have noticed that your code looks a little different from pop culture images of code. Here’s code you’ll recognize from your work in playgrounds:
And here’s code you might have seen on a programmer’s screen in a movie (probably an old movie):
“Hello World!”
Those code samples are exactly the same: They’ll run the same and have the same effects. So what’s up with the green-on-black style?
It goes back to the 1970s and 1980s when computer displays were monochrome and only capable of displaying a single color, usually green on a black screen.
These days, some programmers still choose a black background because it’s easier on their eyes (especially in a dark room)—or just because they like its retro look.
You, of course, can use whatever color scheme helps you write the best code. Head on to the next page and follow the directions to change your Xcode color scheme.
Changing your Colours
Many programmers like to set up their computing environment in a style that feels comfortable. In Xcode, you can make changes to your environment by selecting Xcode > Preferences… from the menu bar:
In the Preferences window, choose the Fonts & Colors tab, and make sure Source Editor is selected:
To get a feel for a totally retro style, choose one of the three options with a dark background: Civic, Dusk or Midnight:
Did this change make the code hard to read? No problem: These are your preferences, and you can make your code look however you’d like. But if you can still read the code with this dark background, stick with it for the next few playground pages—where you’ll add some other old-fashioned effects.
Hiding the Results Sidebar
Remember the trusty results sidebar and how it automatically shows the answers to any calculations you enter? 👉
let sum = 1 + 2 + 3 + 4 + 5 let product = sum * sum
/*: To continue in the spirit of old-time programming, you’ll need to hide that sidebar. Mouse over its left edge slowly, until the cursor turns into a double-headed arrow:
Then click and drag that edge to the right until the sidebar is too small to see the whole calculation result.
Drag it open and shut a few times more, just so you remember how to bring it back later.
- callout(Experiment):
Change some of the numbers above. Change 5 into 23857, change 3 to 11, or to any other number. Without the results sidebar, you have to guess what the result will be.
Do you miss the results sidebar? Even in this old-fashioned world, you still have a powerful tool for investigating the state of your program.
Showing the Console
Show the console by clicking this button in the bottom left of the Xcode window:
or the middle button in the top right of the Xcode window:
Show and hide the console a few times, just to practice, but leave the console showing when you’re done.
To adjust the height of the console, you can drag the bar at the top of the console up and down.
let charactersRemaining = 140 - 97 let micTest = "Is this thing on?"
/*: Even though the code above ran, you’ve received no feedback. The console is empty because it only displays messages when a programmer gives the instruction to print.
Go to the next page for your ceremonial induction into the community of coders 🎓. */
Hello
The print command tells a Swift program to send a message to the console. You can use it with a string like this:
print(“Testing, one two three.”)
You now get to craft the first message you’ll send to the console. Most programmers, whether learning their first programming language or their fiftieth, choose to start with a friendly “hello.”
But not just a generic hello. For their first hello, programmers typically go for the grander gesture and greet the whole world. This is the long-standing tradition you learned about earlier in this lesson.
It’s time now to greet the console with a cheerful “Hello, world!” To truly participate in the ritual, you must use those two words exactly, but it’s OK to punctuate them however you’d like.
Type the print command as in the example code above, with your own “Hello, world!” greeting inside the parentheses.
// Add your greeting just below this line, and watch it appear in the console:
/*: Congratulations and welcome! It may seem like a lot of fuss over a single line of code, but you’re now part of a long programming tradition. Feel free to capture the moment with a selfie of yourself and the screen. */
Messages from Programmers
Why would you want to print to the console when you have a perfectly good results sidebar?
The results sidebar is a feature of playgrounds. When you’re making apps in Xcode, you don’t get a results sidebar.
The results sidebar can only show a small amount of information, on a single line.
If you’re using code that was written by other people, the console is where you’ll see their direct messages about their code. For example, try uncommenting the line below and checking the console output:
//theAnswer
/*: That was a lot of console noise! Remember, you can either drag the top bar up to make the console area bigger, or scroll vertically through the text if it doesn’t all fit.
- note:
Your Xcode setup might open the console every time it has a message. If you like, you can leave it open while you work. But if you find that the console area is getting in the way of your code, you can always drag the top bar to make it shorter.
Next, find out what you can send to the console.
What Can You Print?
Anything that you’d see in the results sidebar, you can print to the console.
Open the results sidebar and confirm this for yourself by checking the examples.
The \n you see in the results sidebar for the print statements is because print adds a new line at the end of the string. Otherwise the console would have everything printed on the same line.
You can print strings:
"Hello, world!" print("Hello, world!") //: Numbers and calculations: 8 print(8) 7 + 11 print(7 + 11) //: String expressions: "Have you seen" + "..." + "nevermind." print("Have you seen" + "..." + "nevermind.") //: Constants: let authorName = "Beatrix Potter" print(authorName) let bookTitle = "Jemima Puddleduck" print(bookTitle) /*: The result of whatever’s inside the parentheses will appear in the console.
- callout(Experiment): Look for the following statements in the results sidebar, then make each statement print to the console, following the example.\ Before:\ `authorName`\ After:\ `print(authorName)` */ authorName "authorName" "Did you know that \(bookTitle) was written by \(authorName)?"
Logging
You may have heard of a captain’s log, where a seafaring (or spacefaring) captain records all the day-to-day info about the running of the ship. But did you know that apps can have logs, too?
When coders print messages to the console, it’s usually to record, or to log, information about a program as it runs. Printing messages to the console is known as logging and the messages are sometimes called log messages.
Programmers often use log messages to indicate that something has gone wrong or that something unexpected has happened. As you’ll see below, messages can provide warnings and help diagnose problems.
Open up the results sidebar, then try changing the numbers in the addToScore() statements below. What’s the maximum number of points you can reach with no printed warnings?
addToScore(1)
addToScore(2)
addToScore(1)
/*: Once you’ve maximized your points, go ahead and hide the results sidebar. You’re going retro again.
Wrapup
The console is a tool programmers use to display information about a program.
You’ve learned how to show the console and how to use print to display all kinds of information in it.
You’ve learned that the results sidebar is not available to you unless you’re in a playground, so print and the console is a great way to find out what’s happening when you’re building an app.
And just in time, too. In the next lesson of App Development With Swift, you’ll get started making your first app.
Continue in this playground for some exercises.