Swift tutorial: Value types

I created this site to help people improve their code. And that’s still the mission today. Over the years, and especially in the recent months, I’ve seen so many young programmers get confused with some of the basic foundations of Swift. That’s why I decided to create this series of swift tutorials explaining the basics of the language.

So, let’s get started!

What is Swift and why Apple created it?

Swift is a programming language created by Apple. It’s easy to learn, but in my opinion difficult to master, however, the easy part will do for most of what you’ll need. Swift is a strongly typed language and has a familiar syntax for developers that come from other famous languages.

Swift was created, among other reasons, to make building apps for the Apple platform beginner friendly. Previously the scene was dominated by Objective-C. A powerful language with a syntax peculiar enough to confuse less experienced developers. Therefore, Apple came with this new pleasant-to-write language, not only attract young developers but also the very experienced ones.

If you want to learn more about what is Swift, you can visit the official website.

What is a type?

A type is an attribute we give to our data to tell the compiler how we intend to use it. In Swift, there are two kinds of data types: the “value” types and the “reference” types.

To keep the right pace, in this article we are going to talk about just value types.

What is a value?

A value is an instance of some data, like a number or text. In Swift, most of the commonly used value types are represented with struct, like Int, Bool, String, etc. But also another common way to represent a value type is enum. Here are some examples of values in Swift:


But what do we do with just values? We need to be able to identify them so we can move them around.

Introducing Variables

So again, what do we do with a value? We store it and run operations on it. How? Well, first we need to label it.

A variable is just a label that helps you identify your value for later use.

var a = "Hello world"

It’s important to understand, that When we run an operation that mutates our value, we don’t change the original, but instead, a new one gets created and replaces the old value. Knowing this will help you decide what kind of data type to use when coding.

var b = ["Hello", "World"]
// b = ["Hello", "World", "!"]


Constants are mostly like variables but, once they’re assigned with a value, they cannot be reassigned. In Swift, they are declared with the keyword let.

let c = 1

Trying to assign c = 2 will result in a compiler error

Passing Values Around

When working with value types, if we assign a variable to another, the value gets copied from one to the other. While the two variables may have the same value, changing one won’t affect the other.

var d = b // ["Hello", "World", "!"]
d.append("from Italy")
// ["Hello", "World", "!", "from Italy"]

Notice how b stays the same, while the value of d changed.


This is all for now about value types in Swift. Hope you liked it and feel excited to go and play with what you’ve learned. On the next part, I’m going to talk about reference types.

If you like what you just read or want me to continue writing about the Swift basics, please subscribe to receive on your email a monthly summary of the activity of the blog. Also, please share this content to keep me motivated. And if you have any questions or something to add, let me know on the comments section below.

Bye 😉

Partager Commentaires