Game Development Reference
In-Depth Information
3.
Select an application from the OS X group.
4.
Select Command Line Tool and click the Next button.
5.
Enter SwiftConsoleApp for the product name.
6.
Enter Apress for the organization name and
com.apress
for the organization
identifier.
7.
Make sure Swift is the selected language, click the Next button, and select a
good place to store your project files.
I chose an OS X command-line tool so you can focus on Swift and not concern yourself
with other distractions. You are now ready to go, but before moving on, let's examine
what you just created.
Let's start with the file
main.swift
. As you will notice, there's not a whole lot to this
file. It contains a single
import
statement used to make all of the Foundation classes
available to this Swift file. After this statement is where you will be adding all of your
code in the following sections.
Note
Throughout this chapter you will be running different examples. When
you run each example, you will need to replace the body of
main.swift
with
the new code.
Variables and Constants
The first things I will talk about are variables and constants. Their syntax is similar except
to declare a variable, you use the keyword
var
, and to declare a constant, you use the
keyword
let
. Take a look at the following two declarations:
var color = "Blue"
let city = "Denver"
The first line of code declares a variable named
color
, and the second line of code de-
clares a constant named
city
. Notice how neither of these declarations explicitly defines
the type. Swift will derive the type based upon what you store in either the variable or the
constant. If you want to define the type, then you do so using the following syntax:
var color : String