Thuta Learning
Swift
BasicProgrammingBeginner

Data Types

SwiftLesson 620 min

What you'll walk away with

  • Explain the type, ownership, and runtime behavior of Data Types
  • Test nil, error, boundary, and concurrency cases
  • Write safe and testable Swift

Explain it simply

Data Types, in plain language

Swift is a type-safe language. That means the compiler always knows exactly what data type a variable holds. This lets it catch mistakes early — like putting text into a number, or a boolean into a string.

Problem → Why → Solution

The problem — why this concept matters

After let age = 24, you can't assign age = "twenty four" — the types don't match, so it will throw an error.

A common mistake

Assuming one sample output proves nil, ownership, actor isolation, and every error path.

Core concept — understand how it works

Treat Data Types as a contract about value and reference semantics, mutability, copying, identity, optionality, and lifetime—not merely syntax. Prefer struct or enum value types and use class identity or shared mutable state only when required.

Mental model
  1. typed value
  2. Swift expression or function
  3. safe result

Explain the type, ownership, and runtime behavior of Data Types

Core syntax or pattern

swift
let name: String = "Nandar"   // Explicit type
let age = 24                 // the compiler infers Int
let rating = 4.8             // Double
let isPremiumUser = true     // Bool

print("\(name) is \(age) years old.")
print("Rating: \(rating), Premium: \(isPremiumUser)")

Treat Data Types as a contract about value and reference semantics, mutability, copying, identity, optionality, and lifetime—not merely syntax. Prefer struct or enum value types and use class identity or shared mutable state only when required.

Read the code step by step

  1. typed valuelet name: String = "Nandar" // Explicit type

    Swift is a type-safe language. That means the compiler always knows exactly what data type a variable holds. This lets it catch mistakes early — like putting text into a number, or a boolean into a string.

  2. Swift expression or functionlet isPremiumUser = true // Bool

    Treat Data Types as a contract about value and reference semantics, mutability, copying, identity, optionality, and lifetime—not merely syntax. Prefer struct or enum value types and use class identity or shared mutable state only when required.

  3. safe resultprint("Rating: \(rating), Premium: \(isPremiumUser)")

    Run and test an original Data Types example with empty, nil, boundary, invalid, error, and cancellation cases. Verify value copying, shared references, actor isolation, and main-actor effects where relevant.

Execution flow
  1. typed value
  2. Swift expression or function
  3. safe result

Success

Explain the type, ownership, and runtime behavior of Data Types

Failure

After let age = 24, you can't assign age = "twenty four" — the types don't match, so it will throw an error.

Swift is a type-safe language. That means the compiler always knows exactly what data type a variable holds. This lets it catch mistakes early — like putting text into a number, or a boolean into a string.

In Swift, you can write out a type explicitly, or let the compiler figure it out from the value — this is called type inference.

swift
let name: String = "Nandar"   // Explicit type
let age = 24                 // the compiler infers Int
let rating = 4.8             // Double
let isPremiumUser = true     // Bool

print("\(name) is \(age) years old.")
print("Rating: \(rating), Premium: \(isPremiumUser)")

String is for text, Int is for whole numbers, Double is for decimal numbers, and Bool is for true/false values. \(...) is called string interpolation, and it lets you drop a variable's value right into a string.

You should see
Nandar is 24 years old. Rating: 4.8, Premium: true

Real-world usage

How to apply it in a real project

Run and test an original Data Types example with empty, nil, boundary, invalid, error, and cancellation cases. Verify value copying, shared references, actor isolation, and main-actor effects where relevant.

Mini challenge

Build it yourself

Run and test an original Data Types example with empty, nil, boundary, invalid, error, and cancellation cases. Verify value copying, shared references, actor isolation, and main-actor effects where relevant.

  • Rebuild the example without copying it
  • Test a normal input and an edge case
  • Predict the compiler or output result before running it
swift
let name: String = "Nandar"   // Explicit type
let age = 24                 // the compiler infers Int
let rating = 4.8             // Double
let isPremiumUser = true     // Bool

print("\(name) is \(age) years old.")
print("Rating: \(rating), Premium: \(isPremiumUser)")

Expected: Explain the type, ownership, and runtime behavior of Data Types

Show hint

Run and test an original Data Types example with empty, nil, boundary, invalid, error, and cancellation cases. Verify value copying, shared references, actor isolation, and main-actor effects where relevant.

Quick quiz 1 / 3

What best describes the core idea of Data Types?

Quick quiz 2 / 3

Which mistake should you avoid first?

Quick quiz 3 / 3

How should this be applied in a real project?

What to remember

  • typed value → Swift expression or function → safe result
  • Treat Data Types as a contract about value and reference semantics, mutability, copying, identity, optionality, and lifetime—not merely syntax. Prefer struct or enum value types and use class identity or shared mutable state only when required.
  • After let age = 24, you can't assign age = "twenty four" — the types don't match, so it will throw an error.
  • Run and test an original Data Types example with empty, nil, boundary, invalid, error, and cancellation cases. Verify value copying, shared references, actor isolation, and main-actor effects where relevant.

The Swift Programming Language — Swift.org

Easy traps

  • After let age = 24, you can't assign age = "twenty four" — the types don't match, so it will throw an error.
  • Assuming one sample output proves nil, ownership, actor isolation, and every error path.
  • Bypassing compiler safety with force unwraps or unchecked shared mutable state.

Hands-on Exercise

Run and test an original Data Types example with empty, nil, boundary, invalid, error, and cancellation cases. Verify value copying, shared references, actor isolation, and main-actor effects where relevant.

You'll know it worked when: Nandar is 24 years old. Rating: 4.8, Premium: true

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

Data Types | Thuta Learning