No history yet

Swift Basics

Welcome to Swift

Swift is the programming language created by Apple to build apps for all its platforms, from iPhones to Mac computers. It’s designed to be safe, fast, and easy to read. Think of it as a set of instructions a computer can understand. We'll start with the most basic building blocks.

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

Storing Information

In programming, we need places to store information, like a username or a score in a game. We use variables and constants for this.

A constant is a value that, once set, cannot be changed. You use the let keyword to declare a constant. Think of it as engraving a name in stone. It's permanent.

let maxScore = 100
// maxScore is now 100 and can never be changed.

A variable, on the other hand, can be changed after it's set. You use the var keyword for variables. This is like writing a score on a whiteboard. You can erase it and write a new one.

var currentScore = 0
// currentScore starts at 0

currentScore = 15
// Now, currentScore is 15.

A good rule of thumb is to always use let unless you know you'll need to change the value later. This makes your code safer and easier to understand.

Types of Data

Swift needs to know what kind of data you're storing. Is it text? A whole number? A number with a decimal? A true/false value? This is called a data type. Here are the most common ones:

  • String: A piece of text, like a name or a message. You wrap strings in double quotes.
  • Int: An integer, which is a whole number like 10, -5, or 0.
  • Double: A number with a fractional component, like 3.14 or -0.5. It's used for more precise values.
  • Bool: A boolean value, which can only be true or false. It's perfect for tracking states, like whether a user is logged in.
// Swift can often figure out the type on its own (type inference)
let name = "Alice"        // String
let age = 30             // Int

// But you can also be explicit about the type
let pi: Double = 3.14159
let isLoggedIn: Bool = true

Notice the colon after the variable name in the last two examples. That’s how you explicitly tell Swift what the data type is. This is called type annotation.

Making Decisions and Repeating Tasks

Programs aren't just lists of instructions executed in order. They need to make decisions and perform repetitive tasks. This is called control flow.

To make a decision, you use an if statement. It checks if a condition is true and runs a block of code accordingly. You can also provide an else block to run if the condition is false.

let temperature = 25 // in Celsius

if temperature > 20 {
    print("It's a warm day!")
} else {
    print("It's a bit chilly.")
}

To repeat tasks, we use loops. A common type is the for-in loop, which runs a block of code for each item in a collection or a range of numbers.

// This loop will print numbers 1 through 5
for number in 1...5 {
    print("Number is \(number)")
}

Bundling Code with Functions

As your programs grow, you'll find yourself writing the same code over and over. Functions let you package up a piece of code, give it a name, and run it whenever you want. It's like creating your own custom tool.

A function can take in data (called parameters) and can also give back data (called a return value).

// This function takes a person's name and returns a greeting
func greet(person: String) -> String {
    let greeting = "Hello, " + person + "!"
    return greeting
}

Let's break that down:

  • func is the keyword to start a function.
  • greet is the name of our function.
  • (person: String) is the parameter. It's named person and must be a String.
  • -> String tells us the function will return a String value.
  • The code inside the curly braces {} is what the function does.

To use the function, you call it by its name and pass in the required data.

let message = greet(person: "David")
print(message) // Prints "Hello, David!"

You've just learned the core fundamentals of Swift. Now, let's test your knowledge.

Quiz Questions 1/6

In Swift, what keyword is used to declare a value that cannot be changed once it's set?

Quiz Questions 2/6

Which data type is most appropriate for storing the value true?

With these concepts, you have a solid foundation for building more complex logic and, eventually, full applications.