Link Menu Expand Document

Swift Programming

Variables Series · Lesson 3

Data Types in Swift

Understand why type choices matter and how they influence memory, operations, and correctness.

Swift Programming Xcode iOS

What to focus on while watching

  • Each variable stores a specific kind of data such as Int, String, or Bool.
  • Type safety helps prevent invalid operations at compile time.
  • Choosing the right type improves reliability and readability.

Lesson Notes and Walkthrough

A data type tells Swift what kind of value a variable stores. Types help Swift prevent invalid operations and keep your app behavior predictable.

Common Types You Will Use First

  • String: text, for example names or messages.
  • Int: whole numbers, for example score or age.
  • Double: decimal numbers, for example price or distance.
  • Bool: true/false values, for example isLoggedIn.

Step-by-Step Example

var studentName: String = "Lina"
var lessonCount: Int = 4
var progress: Double = 0.75
var isComplete: Bool = false

print(studentName)
print(lessonCount)
print(progress)
print(isComplete)

Why Type Safety Helps Beginners

If you accidentally do something invalid, Swift warns you early. For example, adding text to an Int without conversion will fail at compile time instead of causing hidden bugs later.

Quick Rule of Thumb

  1. If it has letters, start with String.
  2. If it is a whole count, use Int.
  3. If it needs decimals, use Double.
  4. If it is yes/no, use Bool.

You may also like

Practice: Data Types

~6 min Beginner

Goal: Declare variables with different Swift data types and print them.

Steps

  1. Create one String, one Int, and one Bool variable.
  2. Use string interpolation to print all three values.
  3. Check that each value displays in the expected format.

Expected Output

Name: Maya, Age: 21, IsStudent: true
Need a hint? Try values like "Maya", 21, and true.

Code

import Foundation

var name: String = "Maya"
var age: Int = 21
var isStudent: Bool = true

print("Name: \(name), Age: \(age), IsStudent: \(isStudent)")