Swift Data Types: A Complete Guide
Understanding Data Types in Swift
When developing applications in Swift, variables play a crucial
role in storing and managing data. A variable is essentially a
reserved memory location where data can be stored and manipulated.
The type of data stored in a variable determines how much memory
is allocated and what kind of operations can be performed on it.
Swift provides built-in data types that allow developers to store
various types of data, including numbers, text, and Boolean
values. Choosing the right data type ensures efficient memory
usage and enhances application performance.
Types of Data in Swift
In Swift, variables store data in memory, and each variable has a data type that determines what kind of values it can hold. Swift provides a variety of built-in and user-defined data types to handle different types of information efficiently.
Built-in Data Types in Swift
Swift offers a rich selection of primitive data types, each serving a unique purpose:
1. Integer (Int, UInt)
Used for storing whole numbers.
- Int32, Int64 for 32-bit and 64-bit signed integers.
- UInt32, UInt64 for unsigned integers.
- Example: var score: Int = 100
2. Float
A 32-bit floating-point number, ideal for smaller decimal values.
- Example: var pi: Float = 3.14
3. Double
A 64-bit floating-point number, suitable for high-precision calculations.
- Example: var value: Double = 3.1415926535
4. Boolean (Bool)
Stores true or false values.
- Example: var isActive: Bool = true
5. String
A collection of characters enclosed in double quotes.
- Example: var message: String = "Hello, Swift!"
6. Character
A single-character value.
- Example: var letter: Character = "A"
7. Optional
Represents a variable that may or may not contain a value.
- Example: var name: String? = nil
Memory Allocation & Value Range
Swift automatically allocates memory based on data types. Here’s a breakdown:
Data Type | Memory Size | Value Range |
---|---|---|
Int8 | 1 byte | -127 to 127 |
UInt8 | 1 byte | 0 to 255 |
Int32 | 4 bytes | -2,147,483,648 to 2,147,483,647 |
UInt32 | 4 bytes | 0 to 4,294,967,295 |
Int64 | 8 bytes | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
UInt64 | 8 bytes | 0 to 18,446,744,073,709,551,615 |
Float | 4 bytes | ~6 decimal precision |
Double | 8 bytes | ~15 decimal precision |
User-Defined Data Types in Swift
Swift allows developers to create custom data types using:
1. Structures (struct)
Best for storing simple data with value semantics.
struct
Person {
var name:
String
var age:
Int
}
var user
=
Person(name:
"John", age:
30)
2. Classes (class)
Used for complex objects and reference-based types.
class
Student {
var name:
String
var age:
Int
init(name:
String, age:
Int) {
self.name
= name
self.age
= age
}
}
var
studentInfo
=
Student(name:
"Ahmed", age:
27)
3. Enumerations (enum)
Used to define a set of related values.
enum
CompassDirection {
case north,
south, east,
west
}
var
direction
=
CompassDirection.north
4. Protocols
Define a blueprint for methods and properties.
protocol
Identifiable {
var id:
String {
get }
}
Type Safety in Swift
Swift is a type-safe language, meaning it prevents type mismatches at compile time. For example:
var number: Int = 10
// number = "Hello" // ❌ Error: Cannot assign String to Int
Type Inference in Swift
Swift automatically detects the type of a variable without explicit declaration:
var count = 100 // Inferred as Int
var pi = 3.14 // Inferred as Double
var greeting = "Hello" // Inferred as String