Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

  1. Home
  2. SwiftBeginner - What are Data Types?

What are Data Types? - Images: Japanese

Hey there, everyone!

Next up, let's dig into Data Types — also just called types for short.

This is one of those topics notorious for tripping up beginners, and Swift is very strict about data types. So there's no getting around it. It can be a bit tough, but let's push through together.

First, what even is a 'data type'? A data type is a way of classifying the different kinds of data used in a program — think of it like putting data into categories.

We've already worked with numbers and strings in previous articles. Whole numbers use something called the Int type, and strings use the String type internally — both of these are data types.

In Swift, when you define a variable or constant, you generally need to specify its type upfront.

Variables and constants are covered in the next article.

So why do you need to declare a type ahead of time? There are a few reasons, but the main one is to reserve memory in advance.

If that sounds a bit abstract, let's take a quick look at how your computer or smartphone actually works under the hood.

Right now you're using a PC or smartphone to read this site, and both of those devices have a component called 'memory'. Every program stores temporary data in this memory in order to do its work.

To save information to memory, you need to specify a write location (an address) and the actual data to write (binary made up of 0s and 1s).

In the old days, programmers had to specify all of that manually. These days, almost every modern programming language handles it automatically behind the scenes — and Swift is no exception.

Super convenient, right? But this convenience comes with a small catch.

Since memory is shared by the OS and other applications running at the same time, your program needs to claim its chunk of memory before it needs it.

Imagine your program tries to write some data to memory, only to find that another application has already taken that spot — that's a problem. And what happens when a program can't write to memory? It crashes.

You've probably heard someone cry "it crashed because of low memory!" — that's essentially what's happening. The program tried to write to memory but the space was either taken or simply not available.

So for a program to run reliably, something has to figure out in advance "how much memory does this program need?" and reserve that space ahead of time.

You might think "why not just reserve 1 GB of memory to be safe?" — but that's not a great idea.

A program that hogs way more memory than it needs, or puts unnecessary strain on the CPU, ends up being painfully slow and basically unusable.

Both memory and CPU are finite resources, so the ideal is to "accomplish the goal while using as little memory and CPU as possible."

Memory is a storage device that, like a hard disk drive or SSD, stores information as binary data made up of 0s and 1s.

However, there's one big difference: unlike an HDD or SSD, memory only holds information while the device is powered on.

Turn the power off, and everything stored in memory is wiped clean. That might sound alarming at first.

But think about it — most of the data that an OS, software, or app handles doesn't actually need to be stored permanently. So almost all of it is kept temporarily in memory while it's being processed.

If HDDs and SSDs hold data permanently, why use memory at all? It seems like they'd be more useful, right?

The answer comes down to speed.

A typical HDD runs at around 140 MB/s — meaning it can write about 140 MB of data per second. That's actually on the faster end for HDDs, but let's compare it to memory.

Even an older DDR3-1333 type memory module runs at 12.8 GB/s — an entirely different order of magnitude.

And the memory found in something like the PS4 can hit 176 GB/s — absolutely blazing fast. There's simply no comparison.

That's why OSes and applications primarily rely on memory for active processing.

Also, when you think about it, the amount of data that truly needs to be saved permanently is actually pretty small — which is another reason memory is the go-to choice for temporary operations.

Figuring out which data you can throw away and which you need to keep is complicated. It's just easier to use memory from the start and let it disappear when you're done.

Okay, that was a long detour — but back to the point. Data types are what's used to determine how much memory a program needs to reserve.

Let's look at a quick example. Here's a variable defined with the Int type:

var n: Int // Define variable 'n' as Int type.

When you do this, variable 'n' is defined as an Int. On a modern Mac (64-bit machine), Swift's Int type can represent values from '-9223372036854775808' to '9223372036854775807', using 8 bytes of storage.

In other words, reserving 8 bytes (64 bits) of memory for variable 'n' is all you need — no memory-related issues will occur.

By declaring a type upfront like this, Swift can calculate exactly how much memory the program needs at build time, generate a program that reserves only the minimum required memory, and run it efficiently.

Types are also used to tell the computer how to interpret data. Since virtually everything in a computer is binary (0s and 1s), the type tells Swift whether something like 1001 should be treated as the letter a or the number 1.

This is similar to the concept of file extensions. A .txt extension means treat the data as text; .jpg means treat it as an image — same idea.

Build refers to the process of compiling and transforming source code into an executable form through various conversions.

We've run Swift in Xcode several times already. Xcode runs a quick build in real time as you type.

That's why writing something like print() produces output almost immediately.

By the way, if you were building a program without a feature-rich editor like Xcode, the workflow would look something like: "write source code" → "run a compile command" → "check the result" → "write more." A bit more hands-on, but that's how things used to be done back in the day.

Compile refers to translating the source code you've written into machine language — binary that the processor can directly understand (built from 0s and 1s).

The software that does this translation is called a compiler.

It's a term you'll hear a lot, so keep it in mind.

These days, there are plenty of languages where you don't need to specify types at all — JavaScript, PHP, and Ruby are well-known examples.

This works because even without the programmer explicitly declaring a type, the language handles memory management automatically behind the scenes at runtime or compile time.

The upside of these languages is that you can write code quickly, and the learning curve for beginners tends to be lower.

The tradeoff is that there's extra processing happening under the hood to manage memory, which can make execution slightly slower and may use more memory than strictly necessary. So there are pros and cons either way.

Alright, this article got pretty long, but let's wrap up with a quick rundown of the basic data types you'll use in Swift.

First, for numeric types. The type for representing positive and negative whole numbers is Int.

In earlier articles, we wrote numbers directly and printed them with print() — when you do that, Swift treats them as Int (specifically Int64) internally.

print(1) // Treated as Int type internally.

var n: Int // Define variable 'n' as Int type.

For strings, the type you'll use most often is String.

When you wrap something in double quotes without specifying a type, Swift treats it as String internally — keep that in mind.

print("Hello world") // Treated as String type internally.

var s: String // Define variable 's' as String type.

For true/false values — boolean values — the type used is Bool. This applies whether you write the value directly or declare a variable.

print(true) // Treated as Bool type internally.

var b: Bool // Define variable 'b' as Bool type.

For 'floating-point numbers' (decimals) like '3.14' or '0.12', the type to use is 'Double'. Note that the Int type cannot represent decimal values, so be careful about that.

print(3.14) // Treated as Double type internally.

var d: Double // Define variable 'd' as Double type.

That covers the basic data types in Swift. These should be enough to get you through for now.

There's one thing to be aware of when working with floating-point numbers in a computer. Take a look at this example:

var n: Double = 0.21
var _n: Double = 0.20

print(n - _n)

We'll cover variables in detail in the next article, but this is a simple program that just calculates "0.21 - 0.20" and prints the result. The answer should be "0.01" — but if you actually run it, something unexpected happens.

0.00999999999999998

A strange value comes back.

This happens because decimal floating-point numbers can't always be converted cleanly into binary, so small errors creep in internally.

This isn't unique to Swift — it happens in other languages too. Computers have always had a hard time with floating-point arithmetic. It's an inherent limitation of a world where everything is represented as 0s and 1s.

The key takeaway: floating-point numbers can produce small internal errors, so keep that in mind when working with them.

That was quite a long one, but that's it for data types! In the next article, we'll look at variables and constants with data types in mind. See you there!

This article was written by Sakurama.

Author's beloved small mammal

桜舞 春人 Sakurama Haruto

A Tokyo-based programmer who has been creating various content since the ISDN era, with a bit of concern about his hair. A true long sleeper who generally feels unwell without at least 10 hours of sleep. His dream is to live a life where he can sleep as much as he wants. Loves games, sports, and music. Please share some hair with him.

If you find any errors or copyright issues, please .