top of page
3DaysOfSwift_final_cleaned_Logo-transparent-v3 ⭐️-tiny.png

Free iOS interview prep - View our homepage & get the full experience

Chapter 1 - The Basics

  • Jun 5
  • 8 min read

Updated: Jun 7

The Swift Programming Language Book

Revision Guide


Chapter 1

The Basics



This chapter introduces the fundamental building blocks of the Swift language, including constants, variables, numeric types, type safety, optionals, error handling, and assertions.



Constants and Variables


Constants and variables associate a name with a value.


A constant cannot be changed after it receives a value.


A variable can be assigned a different value later.



Declaring Constants and Variables


Use let to declare a constant.


Use var to declare a variable.

In this example:


  • maximumNumberOfLoginAttempts is a constant because its value never changes.

  • currentLoginAttempt is a variable because its value changes over time.


If a value does not need to change, prefer a constant.


Constants make code safer and communicate intent more clearly.



Delayed Initialization


A constant or variable can be declared without an initial value and assigned later.


Before a value can be be read, Swift requires it to be initialized.


A constant can be assigned exactly once.

In this example, the constant's value depends on the environment.


Swift guarantees that every execution path assigns a value before the constant can be used.



Multiple Declarations


Multiple constants or variables can be declared on a single line.


Type Annotations


A type annotation explicitly declares the type a constant or variable can store.


Use a colon followed by the type name.

This can be read as:


Declare a variable named welcomeMessage of type String

The declaration above creates a variable that can store String values.


The variable can be assigned a value later.


Multiple Variables


Multiple variables of the same type can share a single type annotation.

Each variable in this declaration stores a Double value.



When To Use Type Annotations


Swift can often determine a type automatically using type inference.


Type annotations are useful when:


  • The type cannot be inferred clearly.

  • You want to make the intended type explicit.

  • You are declaring a value before assigning its initial value.

In this example, Swift knows the constant will store an Int value even though a value has not yet been assigned.



Naming Constants and Variables


Choose names that clearly describe the value they represent.


Swift names can contain almost any Unicode character, including non-Latin characters and emoji.


Naming Rules


Names can contain:


  • Letters

  • Numbers

  • Unicode characters

  • Emoji


Names cannot:


  • Contain whitespace characters

  • Begin with a number

Swift is a case-sensitive language.


These names are different:


Reserved Keywords


Swift reserves certain words for its own use.


Examples include:

  • class

  • struct

  • enum

  • protocol


These keywords cannot normally be used as names.


Using Keywords As Names


If necessary, a reserved keyword can be used as a name by surrounding it with backticks.


This allows the keyword to be treated as an identifier instead of a Swift language keyword.



Naming Conventions


Use names that communicate intent clearly.


A good name should help explain the purpose of the value without requiring additional comments.

Descriptive names are generally preferred over short or ambiguous names.



Printing Constants and Variables


Use print() to display the current value of a constant or variable.

Output:

The value stored in friendlyWelcome is written to the console.



String Interpolation


String interpolation inserts a constant, variable, expression, or function call directly into a string.


Use the following syntax:

Example:

Output:

Swift evaluates the expression and inserts the result into the string.



Expressions in String Interpolation


String interpolation is not limited to variables.


Expressions can also be evaluated.

Output:

The expression is evaluated before the final string is produced.



Why String Interpolation Is Preferred


String interpolation is the standard way to combine values and text in Swift.


It improves readability and avoids manually joining strings together.


Comments


Comments describe code and are ignored by the Swift compiler.


Use comments to explain intent, assumptions, or important implementation details.



Single-Line Comments


Single-line comments begin with //.

Everything after // on the current line is treated as a comment.



Multi-Line Comments


Multi-line comments begin with /* and end with */.

Multi-line comments can span multiple lines.



Nested Comments


Unlike many programming languages, Swift supports nested multi-line comments.


Nested comments allow blocks of code that already contain comments to be commented out safely.


This is particularly useful when debugging or temporarily disabling sections of code.



Semicolons


Semicolons are optional in Swift.


Most Swift code places one statement per line and does not use semicolons.


Multiple Statements On One Line


Use a semicolon to separate multiple statements written on the same line.

In this example, the semicolon separates two statements.



Modern Swift Style


Modern Swift code rarely uses semicolons.


Writing one statement per line is generally preferred because it improves readability.



Integers


An integer is a whole number with no fractional component.


Examples include:

Swift provides signed and unsigned integer types of different sizes.



Integer Bounds


Every integer type has a minimum and maximum value it can store.


Use the min and max properties to access these values.

Output:

Output:


A UInt8 value can store any number between and 255.


Attempting to store a value outside the valid range results in an error.



Int


Int is Swift's default integer type.


Its size matches the native word size of the current platform.


Modern Apple platforms use a 64-bit architecture, so Int is typically a 64-bit integer.


In most situations, Int should be your preferred integer type.



UInt


UInt is Swift's default unsigned integer type.


Unsigned integers can store only positive values and zero.

Because UInt cannot represent negative values, assigning a negative number produces an error.

Swift recommends using Int unless you have a specific reason to use an unsigned integer type.



Floating-Point Numbers


Swift provides two floating-point types:

  • Double

  • Float



Double


Double is a 64-bit floating-point number.


It provides greater precision than Float.


Swift recommends using Double unless a specific reason exists to use Float.



Float


Float is a 32-bit floating-point number.


It uses less memory but stores fewer decimal digits of precision.



Type Safety and Type Inference


Swift is a type-safe language.


Type safety prevents values of incompatible types from being used together accidentally.



Type Inference


Swift can often determine a value's type automatically.


This process is known as type inference.

Swift infers that meaningOfLife is an Int.

Swift infers that pi is a Double.



Mixed Numeric Values


When integer and floating-point literals appear together, Swift infers a type that can represent both values.

Swift infers that anotherPi is a Double.


Type inference reduces repetition while preserving type safety.



Numeric Literals


Numeric literals are values written directly in source code.



Decimal


Binary


Octal


Hexadecimal

All four examples represent the decimal value 17.



Readable Numeric Literals


Underscores improve readability and are ignored by the compiler.


Numeric Type Conversion


Swift does not perform implicit numeric conversions between different numeric types.


Values must be converted explicitly.



Integer Conversion


Different integer types cannot be mixed automatically.


Integer and Floating-Point Conversion


Integers and floating-point values must also be converted explicitly.


Why Explicit Conversion Exists


Swift requires conversions to be visible in code so that changes in precision or range are never hidden.



Type Aliases


A type alias creates an alternative name for an existing type.


Use the typealias keyword.

Type aliases improve readability by expressing intent without creating a new type.



Booleans


Swift provides the Bool type for working with logical values.


A Boolean value can be either:


  • true

  • false



Conditional Statements


Conditional statements require a Boolean value.


Type Safety and Booleans


Unlike some languages, Swift requires actual Boolean values in conditional statements.

Integers are not automatically treated as Boolean values.



Tuples


Tuples group multiple values into a single compound value.


Decomposing Tuples

Each value is assigned to a separate constant or variable.



Ignoring Values


Use an underscore (_) to ignore values that are not needed.


Accessing Values by Position


Named Tuples

Named values can be accessed directly.

Tuples provide a lightweight way to group related values together without creating a custom type.



Optionals


An optional represents a value that may be present or absent.


A non-optional value must always contain a value.


An optional can contain:

  • A value

  • nil

The question mark (?) indicates that the value is optional.



nil


Assign nil to indicate the absence of a value.


Only optional values can be assigned nil.



Optional Binding


Optional binding safely extracts a value from an optional.

If the optional contains a value, the binding succeeds.


If it contains nil, the binding fails.



Multiple Optional Bindings

Each condition must succeed before the body executes.



Providing a Fallback Value


Use the nil-coalescing operator (??) to provide a default value when an optional contains nil.

If the optional contains a value, that value is used.


Otherwise, the fallback value is used.



Force Unwrapping


Force unwrapping accesses the value stored inside an optional.


Use an exclamation mark (!).


Important


Force unwrapping assumes that a value exists.


If the optional contains nil, the application terminates at runtime.

Only use force unwrapping when you are certain a value exists.



Thinking About Force Unwrapping


Force unwrapping can be viewed as a shorthand way of saying:


A value must exist here, or else the program cannot continue

Conceptually, this behaves similarly to:


If the optional contains a value, execution continues normally.


If the optional contains nil, execution terminates with a runtime error.



Implicitly Unwrapped Optionals


An implicitly unwrapped optional is still an optional value.


Use an exclamation mark (!) in the type declaration.

Unlike a normal optional, Swift automatically force unwraps an implicitly unwrapped optional each time it is accessed.


This allows the value to be used without writing ! explicitly.


Important


Every access performs an implicit force unwrap.


If the value contains nil when it is accessed, a runtime error occurs.


Conceptually, every access behaves as though Swift had written:

Use implicitly unwrapped optionals only when a value may be nil initially but is guaranteed to contain a value before it is accessed thereafter.


You may encounter implicitly unwrapped optionals when unit testing and setting the optional value as the system under test.



Error Handling


Error handling allows code to respond to recoverable failures at runtime.


Functions that can throw errors are marked with the throws keyword.

Use do-catch to handle errors.

If an error is thrown, execution transfers to the matching catch block.



Assertions and Preconditions


Assertions and preconditions allow assumptions to be expressed directly in code.


If an assumption is violated, execution stops and reports the problem.



Assertions


Use assertions to detect programming mistakes during development.

Assertions help identify bugs closer to their source.



Preconditions


Use preconditions when a requirement must be true before execution can continue safely.

Examples include:


  • Valid array indexes

  • Required input values

  • Conditions that would make further execution unsafe



Assertions vs Preconditions


Use an assertion to detect mistakes during development.


Use a precondition to enforce requirements that must always be satisfied for the program to continue safely.




Read the Original Swift.org Chapter

You can find the original and official chapter here - The Basics.





We Converted The Swift Programming Language Book into Xcode Playgrounds

You can download the Xcode playground or view the GitHub repo below.






© 2026 3DaysOfSwift.com. All rights reserved.

Stay Interview Ready.

Don't abandon the technical depth you spent years building.

👩🏿‍💻🧑🏻‍💻🙋🏿‍♀️🧑🏼‍💻👩🏼‍💼👩🏽‍💻🧑🏿‍💻💁🏼‍♀️👩🏼‍💻👨🏼‍💻👨🏽‍💻🙋🏽‍♂️👩🏻‍💻🧑🏾‍💻👩🏻‍💻👩🏾‍💻👨🏼‍💻🙋🏻‍♂️👨🏿‍💻🙋🏼‍♂️


Google Search Terms - SEO Keywords

iOS Developer interview prep. Interview preparation. Revise Swift language features. Swift crash course. How to remind myself of Swift syntax. Swift interview material. How to revise for an iOS interview. iOS interview prep. iOS interview questions. last minute revision for interview. What do I need to do for an iOS interview. Help, I have forgotten how to code in Swift. Swift coding interview prep. Modern iOS interviews. iOS interview platform. Where can I remind myself of how to program Swift? iOS interview platform. What questions do they ask in an iOS interview? Last minute iOS interview revision. Learn Swift. The Swift programming language. learn swift everyday. Apple. iPhone. iOS. iOS Developers. Learn to build apps. Become an iOS Developer. Career building in iOS. Write code in Swift. How to learn the swift computer programming language. How to write code in Xcode? Online course teaching Swift. Online course teaching the Swift programming language. Swift online tutorials. Learn Swift online. Swift.org. Official Swift Language Documentation. https://docs.swift.org/swift-book/documentation/the-swift-programming-language/

 

3DaysOfSwift.com is a professional Swift revision platform built for modern iOS developers who want to remain valuable, technically sharp, and highly employable in an AI-assisted software industry.


We are not affiliated with 100 Days of Swift. If you want to learn SwiftUI please visit HackingWithSwift.com.

Apple developer tutorials for SwiftUI can be found here.

Copyright 2026 www.3DaysOfSwift.com. All rights reserved.



Comments


Commenting on this post isn't available anymore. Contact the site owner for more info.
bottom of page