Swift Programming For Complete Beginners Part 1:

Hello, Xcode!

You've decided you want to learn programming. Maybe you have an idea for an app, maybe you're looking to change careers, or maybe you just think it's something you would enjoy doing. Whatever the reason, welcome!

You may have also decided you want to learn Swift and already know why. If so, great! You're in the right place. If not, I wrote another post, Why Learn Swift?, which I hope will help you consider whether it's the right first language for you.

In this article, we'll start from the beginning. You've never written a line of code in your life. You've never taken a computer science course. You've heard the words "Swift", "dictionary", and "algorithm" before, but the first one's a singer, the second one is a book with definitions, and the third is some black magic that powers Spotify Discover.

If that sounds like you, again welcome! If you just rolled your eyes but still want to read on, I promise we'll get to the good stuff soon and hope you'll still learn something.

What Is Swift?

First, a very brief overview of Swift and why you would want to learn it. Swift is a programming language released by Apple in 2014. Currently, it is mainly used in programming for Apple devices (iPhone, iPad, Mac, Apple Watch, Apple TV). However, Swift is also being used to develop websites, web apps, and APIs, and Google is rumored to be considering it as an option for developing Android apps. In fact, the website you're reading was built in Swift!

Swift replaces a language called Objective-C, which previously had been the main language used in Apple device programming. The transition from Objective-C to Swift is still happening, but it is now entirely possible to be a professional engineer specializing in Apple development without knowing any Objective-C.

Again, for more detail on Swift and the difference between websites, web apps, and APIs, see my other article here.

What is Xcode?

The title of this article is "Hello, Xcode!". It's a play on the phrase "Hello, World!", which is often the first program you write when learning a programming language (we'll keep that tradition here).

Xcode is an Integrated Development Environment, or IDE, created and maintained by Apple. An IDE is simply an application with the tools needed to build other applications. In many languages, an IDE is helpful but not necessary. While in theory that's true of Swift as well, when programming for Apple devices Xcode is a necessity, especially for new programmers. I have only rarely written Swift without using Xcode.

So let's download it! Xcode is free and available on the Mac App Store here.

When you first open Xcode, you should see this screen:

Select "Create a new Xcode project". Select the "macOS" tab and click "Command Line Tool". You should see this screen:

Click "Next", enter "HelloXcode" for "Product Name", "com.yourfullname" for "Organization Identifier", and click "Next". Xcode will ask you where you want to save the project. Select a location and click "Create".

When we finish, Xcode will look like this. If your general layout does not look like this, note the buttons in the image that are blue and make sure your buttons match.

The four marked panels are as follows:

  1. Project Navigator: This is where your files are organized. Note that the left-most button in this part of Xcode is selected. There are 7 other tabs, which you don't need to worry about yet.
  2. Standard Editor: This is where you edit your files. It's where you write your code, build the user interface of applications, organize icons that are used in your apps, and complete other tasks depending on the type of the file selected in the Project Navigator.
  3. Console: This is where you'll see the output of your program as well as the logs, warnings, and errors Xcode outputs when you run the program.
  4. File Inspector: This is where you edit settings related to the current file. We won't be working with this area yet.

Your First Program: Hello, World!

I chose to start with the Command Line Tool template because it will allow us to focus on the Swift language. It is the simplest program we can run in Xcode. iPhone, Mac, TV, and Watch apps all have a user interface, which refers to the parts of the application with which humans interact (buttons, text fields, images, toolbars, etc.).

Command Line Tools do not have a user interface. Your Mac comes with a program called "Terminal", which is what we refer to as the "Command Line". Terminal provides a user interface for running command line programs, but we can also run them here in Xcode. As we write our first command line programs in Xcode, we'll see the output directly in the Console.

You'll notice that Xcode already generated a line of code when you created the project.

print("Hello, World!")

Delete everything in the file and retype the above line yourself. As you go through programming tutorials, you'll retain more of what you learn if you type lines yourself instead of copying and pasting from the tutorial.

Now run the program by clicking the play button in the upper left. You should see the output in the Console shown in the screenshot above. Now type the following line right below the first one:

print("Hello, Xcode!")

Run the program again. You should now see both lines printed in the console.

Congratulations, you just wrote a complete computer program! It may not feel like a lot, and you probably have a lot of questions, but you've already taken an important step toward learning to code.

Terminology Review

Review Questions

You should now be able to answer these questions:

In this tutorial you set up Xcode and wrote two lines of code, each of which output a line of text to the Console. Next we'll look at Constants and Variables.