No history yet

Introduction to Swift

Meet Swift

Swift is a modern programming language created by Apple. It’s the primary language for building apps for the iPhone, iPad, Mac, Apple Watch, and Apple TV. Before Swift, developers mainly used a language called Objective-C, which was powerful but also complex and dated. Apple wanted something better.

Think of it like this: Objective-C is like a classic car. It works, it has character, and it got the job done for decades. But it can be clunky, requires a lot of maintenance, and isn't as safe or efficient as a modern vehicle. Swift is the sleek, new electric car. It’s faster, safer, and much more intuitive to drive.

Swift was designed to be easy to read and write, making it a great first language for anyone new to programming.

Here’s why developers have quickly adopted it:

  • Safe: Swift is designed to prevent common programming errors. It has strict rules that catch mistakes before your code even runs, like trying to use a variable that doesn't have a value. This is like having a co-pilot who points out potential problems before you take off.
  • Fast: As its name suggests, Swift is built for speed. It’s optimized to get the most out of modern hardware, making apps feel responsive and quick.
  • Expressive: The language has a clean, simple syntax. This means you can write code that clearly expresses your intentions without a lot of extra symbols or boilerplate. It reads almost like plain English.

Setting Up Your Workshop

To start writing Swift, you need an Integrated Development Environment, or IDE. This is a software application that provides all the tools you need to write, test, and debug code. For Swift, the standard IDE is Xcode, which is made by Apple and is free to download from the Mac App Store.

Xcode is more than just a text editor. It includes a compiler (which translates your Swift code into instructions a computer can understand), a debugger (for finding and fixing bugs), and an interface builder for designing your app's user interface visually.

Once you have Xcode installed from the Mac App Store, you're ready to create your first project.

Your First Program

In programming, a long-standing tradition is to make your first program display the message "Hello, World!". It's a simple way to confirm that your environment is set up correctly and to see the basic syntax of a new language.

Let's do this in Swift using a feature in Xcode called a Playground. A Playground is an interactive environment where you can write Swift code and see the results immediately, without having to build a full app.

  1. Open Xcode.
  2. From the menu bar, choose File > New > Playground....
  3. You'll be asked to choose a template. Select Blank under the macOS or iOS tab and click Next.
  4. Give your Playground a name, like HelloWorld, and save it.

A new window will appear with some pre-written code. You can delete it and type the following line:

print("Hello, World!")

Let's break this down. print() is a function in Swift that does exactly what it sounds like: it prints whatever you put inside the parentheses to the screen. The text inside the quotation marks, "Hello, World!", is called a string, which is just a sequence of characters.

Lesson image

After you type the code, the Playground should run it automatically. You'll see "Hello, World!\n" appear in the debug area at the bottom of the window or in the results sidebar to the right. The \n just represents a new line character.

Congratulations! You've just written and run your first piece of Swift code. You’ve taken the first step into building applications for millions of devices.

Quiz Questions 1/5

What is the primary role of the Swift programming language?

Quiz Questions 2/5

Which of the following best describes why Swift is considered a 'safe' language?