No history yet

Introduction to Swift

Meet Swift

Swift is the programming language you'll use to build apps for iPhones, iPads, and other Apple devices. It was designed by Apple to be modern, safe, and easy to read. Think of it as a set of instructions a computer can understand. You write the instructions, and the iPhone carries them out.

Swift is a powerful and intuitive programming language developed by Apple for iOS, macOS, watchOS, and tvOS app development.

Let's start with the most basic building blocks: syntax and data types. Syntax is just the grammar of the language—the rules you follow to write valid code. Data types are the different kinds of information you can work with, like numbers and text.

// This is a comment. It's ignored by the computer.

// Creating a variable to store text
var greeting = "Hello, world!"

// Creating a constant to store a number
let score = 100

In Swift, you use var to create a variable, which is a piece of information that can change. You use let to create a constant, which is a value that, once set, cannot be changed. It's a good practice to use let whenever you can, as it makes your code safer and easier to understand.

Working with Data

Swift handles several basic types of data. You've already seen a string of text. Here are the most common ones you'll encounter.

Data TypeDescriptionExample
StringA piece of text."Player One"
IntA whole number.42
DoubleA number with a decimal.3.14159
BoolA true or false value.true

Swift is smart enough to figure out the data type on its own most of the time. When you write let year = 2024, Swift knows year is an Int. This is called type inference.

let playerName: String = "Alice" // You can be explicit...
let playerHealth = 100 // ...or let Swift infer the type (Int)
let isAlive = true // Swift knows this is a Bool

You can combine different pieces of data. For example, you can embed a variable inside a string to create a dynamic message. This is called string interpolation.

let currentLevel = 5 let message = "Welcome to level (currentLevel)!"

Making Decisions

Your app will constantly need to make decisions. This is handled by control flow statements. The most common is the if statement, which checks if a condition is true or false and runs different code accordingly.

Here's how that logic looks in Swift code. You can chain multiple conditions together using else if.

let score = 85

if score > 90 {
    print("You got an A!")
} else if score > 80 {
    print("You got a B.")
} else {
    print("Keep studying!")
}
// This will print "You got a B."

Sometimes you need to check a variable against many possible values. A switch statement is perfect for this. It's cleaner than a long chain of if-else if blocks.

let characterClass = "Wizard"

switch characterClass {
case "Warrior":
    print("You are a master of combat.")
case "Wizard":
    print("You wield powerful magic.")
case "Rogue":
    print("You strike from the shadows.")
default:
    print("You are an adventurer.")
}
// This will print the message for Wizard.

Reusable Code with Functions

When you find yourself writing the same piece of code over and over, it's time to create a function. A function is a named block of code that performs a specific task. You can call the function whenever you need it, which makes your code organized and reusable.

// Defining a function
func greetPlayer() {
    print("Hello, adventurer!")
}

// Calling the function
greetPlayer()

Functions can also take in data, called parameters, and return data as a result. This makes them much more flexible.

// A function with a parameter (name) and a return value (String)
func createGreeting(for name: String) -> String {
    let fullGreeting = "Welcome, " + name + "!"
    return fullGreeting
}

// Call the function and store the result
let personalGreeting = createGreeting(for: "Erik")
print(personalGreeting) // Prints "Welcome, Erik!"

Here, for name: String defines a parameter named name of type String. The -> String part tells Swift that this function will give back, or return, a String value when it's done.

Now let's see how well you've grasped these fundamental concepts.

Quiz Questions 1/6

In Swift, what is the primary difference between a constant declared with let and a variable declared with var?

Quiz Questions 2/6

True or False: The following line of Swift code will compile successfully. let name = "Taylor"; name = "Swift"

This is just the start. With these basics of variables, data types, control flow, and functions, you have the essential tools to start building logic for any program or game.