HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to SwiftVariablesData TypesOperatorsStringsArraysConditions (if / else)Loops (for / while)Functions
REFERENCE
📖 Overview
🖥️ Output
📦 Variables & Types
🔢 Numbers & Math
✍️ String
📚 Array & Dictionary
🔀 Condition & Loop
🧩 Function
Swift›Data Types

Data Types

Int, Double, String, Bool; string interpolation and text↔number conversion.

Simple enough for a 5-year-old to understand

Basic types and interpolation

To embed a value into text, \(expression) is used (string interpolation).

Example code
1
let number = 7
Int.
VariableInt.
2
let decimal = 3.14
Double.
VariableDouble.
3
let text = "Swift"
String.
VariableString.
4
print("\(number) \(decimal) \(text)")
Prints 7 3.14 Swift.
Output / InputPrints 7 3.14 Swift.
VariableOutput / Input
💡 Hint: You embed the value into the text with \(variable).

About this topic

Swift's basic types include Int (integer), Double (floating-point number), String (text), and Bool (true/false). Alongside these, there are collections and optional values. Every value has a type, and this type cannot change during execution.

The most important concept that sets Swift apart from other languages is Optionals. The language itself expresses the "absence" of a value through types: an Int? type either holds an integer or nil. Thus, a null value error turns into a situation where the compiler warns you rather than causing a random crash.

Type conversion in Swift is always explicit. You convert by writing Int(x) or Double(x); the language does not silently convert behind your back. While this might seem like extra typing at first, it prevents a large portion of numerical errors right from the start.

What you will learn

  • Basic types and their use cases
  • Explicit type conversion
  • The Optional concept and nil values
  • Safe unwrap methods
  • The behavior of integer division

Who is this for?

  • Beginners working with the type system for the first time
  • Those unfamiliar with the Optional concept
  • Those encountering errors in numerical conversions

When to use?

Double is used for money, measurements, and ratio calculations; Int is used for quantities, indices, and ID numbers. Decimal is preferred if cent precision is critical in financial calculations.

Optionals are used wherever a field might be empty: a server response, a form field left unfilled by the user, or text whose conversion might fail.

Common mistakes

Wrong

let a = 7
let b = 2
print(a / b)

When two Ints are divided, the result is still an Int and the fractional part is discarded: it prints 3 on the screen, not 3.5. This is not an error; it is a rule of the language. If you want a decimal result, you must convert the values to Double before dividing.

Correct

let a = 7
let b = 2
print(Double(a) / Double(b))

Wrong

let text = "abc"
let number = Int(text)
print(number + 1)

The Int("abc") conversion fails and returns nil; therefore, the number type becomes Int? You cannot perform operations directly on an Optional value, you will get the error "value of optional type 'Int?' must be unwrapped". You must safely unwrap the value before using it.

Correct

let text = "abc"
if let number = Int(text) {
  print(number + 1)
} else {
  print("not a number")
}

Wrong

let input = "abc"
let number = Int(input)!
print(number)

The exclamation mark means "this is definitely populated", and if the value is nil, the app crashes saying "Unexpectedly found nil while unwrapping an Optional value". Forced unwrapping should never be used anywhere you are not certain the value is populated.

Correct

let input = "abc"
let number = Int(input) ?? 0
print(number)

Frequently asked questions

Why do Optionals exist, isn't it easier to just use nil directly?

It looks easy but it is costly: null value errors are the most common cause of crashes in mobile apps. Optionals encode the information "this value might be empty" into the type, forcing the compiler to check it for you; the error is shifted from runtime to compile time.

What is the difference between if let and guard let?

Both safely unwrap an Optional. if let makes the unwrapped value usable only within its own block, while guard let leaves the value usable for the rest of the function and exits early if the condition is not met. guard is more readable for validations at the beginning of a function.

Can't I just always use Double instead of Int?

No. Double stores decimal numbers approximately; it gives unexpected results in large integers and equality comparisons. Values that need to be exact, like quantities and indices, must be Int.

Summary

Basic types are Int, Double, String, and Bool.

Conversions are written explicitly; the language does not convert automatically.

Division of two Ints is still an Int, the fractional part is discarded.

An Optional expresses by type that a value might be empty.

Forced unwrapping (!) carries a risk of crashing; if let or ?? is preferred.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code