No history yet

Swift and SwiftUI Basics

Meet Swift

To build an app for Apple devices, you need to speak the right language. That language is Swift. Created by Apple, Swift is a modern, powerful, and easy-to-read programming language. It’s the foundation for everything you'll build, from a simple button to a complex customer relationship manager (CRM).

Think of Swift as the grammar and vocabulary you'll use to write instructions for the iPhone.

Let's start with the basics. In programming, you need places to store information. Swift gives us two ways to do this: variables and constants.

  • A variable, declared with var, can have its value changed later.
  • A constant, declared with let, has a value that cannot be changed once it's set.
// A constant for a user's name
let userName = "Alex"

// A variable for the user's score, which can change
var userScore = 100
userScore = 110 // This is okay

// userName = "Jamie" // This would cause an error!

Swift also needs to know what kind of data it's storing. Is it text? A whole number? A true/false value? These are called data types. The most common ones are:

  • String for text, like "Hello, world!"
  • Int for whole numbers, like 42 or -10
  • Bool for true or false values

Swift is smart and can usually figure out the data type on its own, a feature called type inference.

Finally, your app needs to make decisions. This is done with control flow statements. The most common is the if statement, which runs a piece of code only if a certain condition is true.

var isLoggedIn = true

if isLoggedIn {
  print("Welcome back!")
} else {
  print("Please log in.")
}

Building Interfaces with SwiftUI

Knowing Swift is like knowing how to make individual bricks. But to build a house, you need a blueprint and a construction method. For iOS apps, that method is SwiftUI.

SwiftUI is a declarative framework. That's a fancy way of saying you describe what you want the user interface (UI) to look like, and SwiftUI figures out how to build it and make it work. You don't have to provide a step-by-step list of instructions.

It’s like ordering a pizza by saying “I want a large pepperoni” instead of telling the chef to get dough, flatten it, add sauce, sprinkle cheese, and place 12 slices of pepperoni on top.

In SwiftUI, every piece of your UI—a piece of text, an image, a button—is called a View. You can customize these views by attaching modifiers to them. Modifiers are like descriptive words that change a view's appearance or behavior. Let's look at an example.

// This describes a piece of text for the screen
Text("Journal Entry")
    .font(.largeTitle) // Modifier: Make the font bigger
    .foregroundColor(.blue) // Modifier: Make the text blue
    .padding() // Modifier: Add some space around it
Lesson image

Of course, apps have more than one element on the screen. To arrange multiple views, SwiftUI gives us layout structures called stacks:

  • VStack: Arranges views vertically, one on top of the other.
  • HStack: Arranges views horizontally, side-by-side.
  • ZStack: Stacks views on top of each other, like layers in a cake.

Making Views Interactive

Static text and images are nice, but apps need to respond to user input. How do we connect our UI to our data so that when the data changes, the UI updates automatically?

SwiftUI has a special tool for this called a property wrapper: @State. By putting @State before a variable declaration inside a view, you're telling SwiftUI to watch that variable. If its value ever changes, SwiftUI will automatically rebuild any part of the UI that depends on it.

This is how you handle data in SwiftUI. It's a simple yet powerful way to create dynamic interfaces. For example, you could have a button that increments a number. Every time the button is tapped, the @State variable for the number changes, and the Text view displaying it updates instantly.

struct CounterView: View {
    // SwiftUI watches this variable for changes
    @State private var count = 0

    var body: some View {
        VStack {
            Text("Count: \(count)")
                .font(.title)

            Button("Tap me!") {
                // Tapping the button changes the state
                count += 1
            }
        }
    }
}

This simple concept is the key to building everything from our personal journal, where typing updates the text on screen, to the CRM, where adding a new contact updates a list.

Let's review what you've learned.

Quiz Questions 1/5

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

Quiz Questions 2/5

Swift can often determine a variable's data type without you explicitly stating it. What is this feature called?

You now have the fundamental building blocks for creating iOS apps: Swift for the logic and SwiftUI for the user interface. With these tools, you can start bringing your ideas to life on the screen.