Creating our program

You might be wondering when we are supposed to actually start writing some Swift code; it is right now. Let's go straight to the code by typing the following code in the editor of our playground:

print("Hello World")

Tip

All related source code for this chapter can be found here: https://github.com/swift-book-projects/swift-3-programming-for-kids/tree/master/Chapter-3

After writing this code in the editor, the playground should run. You can see this by checking whether the play button at the bottom changes to a stop button, or whether the status at the top shows Running instead of Ready. When the playground is done running, you should be able to see the result of our code in the right column. It should be as shown:

"Hello World
"

Don't worry about  being added to your print statement; it is simply Xcode adding it for our convenience. This sequence of characters (  ) will be translated into a new line when our application runs. This means that, if we add multiple print statements, Xcode ensures that they are shown on each of their own lines. Our playground should now look as follows:

Creating our program

What exactly happened here? We used the word print to tell our application that we want to print something to the console. print is actually something called a function, which is why we use parentheses to enclose what we want to give to the function, but we will get back to that in a later chapter. Inside the parentheses, we indicate what we want to print in the console surrounded by double quotes, which is how we define a string in Swift. A string is basically a set of characters (letters, digits, and symbols); for example, the Hello World sentence.

How cool is that? Good job on writing your first line of Swift code! Actually, this is what programming is; you are doing it right now.

A quick look at Xcode errors

Let's continue with writing code by typing in the following line right after our current code:

print("We love Swift programming)

Uh oh, what happened? The playground is beginning to show red symbols in multiple places and we can't see our sentence in the results sidebar:

A quick look at Xcode errors

This is because we made an error and Xcode is now unable to understand the last piece of code we just added. This is normal in programming. We are only humans and, while experience and good software tools can help us, it is expected that we make a lot of errors while writing our code. Luckily for us, Xcode is helping us by letting us know which line we made an error on and, sometimes, Xcode will even be able to fix our errors. Can you spot the error? We forgot to finish our sentence by adding the double quote character ("). Go ahead and fix it by inserting the missing double quote and see if the error disappears. Great, now your playground should look like this:

A quick look at Xcode errors

Good job, you now have two lines of Swift code running in your playground file without any errors. You are definitely off to a good start!

Xcode and autocompletion

You might have noticed a small window popping up while writing our first lines of Swift code. This is a feature called autocompletion. The idea of autocompletion is very similar to what we're used to when writing text messages on our iPhone. Before having written all the characters in a word, the messages app will suggest the word we're trying to write. This comes in handy since you save time by not typing all the characters in a word every single time.

Go to a new line and start writing the pri characters. Note how Xcode will show this small autocompletion window:

Xcode and autocompletion

This is basically Xcode guessing what we're trying to do as we write. In this case, we want to add another print statement, so using the arrows on the keyboard to go up and down, we can select the correct instruction we want to perform. Select the proper instruction by pressing Enter and note how our playground looks:

Xcode and autocompletion

The blue box inside the parentheses is something called a placeholder. This is basically telling us that we need to fill in something inside the parentheses. This makes sense because we want to print something. Let's write our favorite ice cream this time. Xcode will automatically select the placeholder, so you should be able to just start writing inside the parentheses. Start typing the "My favourite ice cream is vanilla" characters, and after writing these characters, pay attention to how the closing parenthesis is colored as compared to the other parentheses in our playground:

Xcode and autocompletion

Do you note the difference? The last one seems to be more gray than black. This is because we haven't confirmed the autocompletion Xcode suggested. You can do this by pressing the Tab key, and the line of code should now be done.

Great job setting up your first playground and writing your first Swift code!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset