TExcelTAD
Home › Swift › Swift Intro

Swift Introduction

Swift is a general-purpose programming language created by Apple and released in 2014. It was designed to replace Objective-C as the main language for Apple platforms, but it is also open source and runs on Linux and Windows. Swift aims to be safe, fast and pleasant to read — code often looks almost like plain English.

What is Swift used for?

  • iPhone and iPad apps (iOS and iPadOS)
  • Mac, Apple Watch, Apple TV and Vision Pro apps
  • Server-side applications (with frameworks such as Vapor or Hummingbird)
  • Command-line tools and scripts
  • Embedded devices, thanks to "Embedded Swift"

Why learn Swift?

Feature What it means for you
Type safety The compiler catches type mistakes before your program runs
Optionals A value that might be missing must be handled explicitly — no surprise null crashes
Modern syntax No semicolons, type inference, closures, string interpolation
Performance Compiled to native machine code through LLVM
SwiftUI A declarative way to build user interfaces for every Apple device

Your first Swift program

In Swift you don't need a main function for a small program: the statements at the top level of a file run from top to bottom. Press ▶ Run to compile the program, then change the text and run it again.

swift · try it yourself
print("Hello, World!")
Hello, World!

print is a built-in function that writes text to the console, followed by a new line.

Tip: In every playground on ExcelTAD you can edit the code and press Ctrl+Enter to run it. The Swift examples are compiled with Swift 6.3 on a real server, so the first run can take a few seconds.

A slightly bigger program

Here is a taste of what is coming in the next chapters: a constant, a variable, a loop and string interpolation.

swift · try it yourself
let language = "Swift"
var year = 2014

print("\(language) was released in \(year).")

for version in 1...3 {
    year += 1
    print("Year \(year): version \(version) of my app")
}
Swift was released in 2014.
Year 2015: version 1 of my app
Year 2016: version 2 of my app
Year 2017: version 3 of my app

Don't worry if every detail is not clear yet:

  • let creates a constant (it can never change), var creates a variable.
  • \(...) inside a string inserts a value — this is called string interpolation.
  • 1...3 is a range containing 1, 2 and 3.

Swift is safe by default

Swift refuses to compile code that could easily lead to bugs. For example, you cannot add a number to a piece of text without saying explicitly what you mean:

swift · try it yourself
let apples = 5
let label = "Apples: "
print(label + apples)
error: emit-module command failed with exit code 1 (use -v to see invocation)
<source>:3:13: error: binary operator '+' cannot be applied to operands of type 'String' and 'Int'
1 | let apples = 5
2 | let label = "Apples: "
3 | print(label + apples)
  |             |- error: binary operator '+' cannot be applied to operands of type 'String' and 'Int'
  |             `- note: overloads for '+' exist with these partially matching parameter lists: (Int, Int), (String, String)

The error is reported before the program runs. Fix it by converting the number with String(apples) or by writing print("Apples: \(apples)").

Swift vs. other languages

Swift Objective-C Kotlin
Released 2014 1984 2011
Main platform Apple devices Apple devices (legacy) Android
Null safety Optionals No Nullable types
Semicolons Optional Required Optional

Test yourself

Which company created Swift?

  1. Google
  2. Apple
  3. Microsoft
  4. JetBrains

What does a small Swift program need before its first statement can run?

  1. A main() function
  2. A class named Program
  3. Nothing — top-level statements run from top to bottom
  4. An import Swift line

Which keyword creates a value that can never change?

  1. var
  2. let
  3. const
  4. final