> For the complete documentation index, see [llms.txt](https://ios-course.cornellappdev.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ios-course.cornellappdev.com/resources/textbook/swift-basics/data-types.md).

# Data Types

In the variables and constants section above, we assigned a text to a variable. In Swift, this is called a **String** and is one of the most important types we will use. However, there are many more types of data that Swift handles. In the example earlier, let’s try to do the following:

```swift
var instructor
instructor = "Vin"
```

<figure><img src="/files/9v2OeHeoweRwEUFyeLza" alt=""><figcaption></figcaption></figure>

There are two ways we can fix this error:

1. Initialize the variable with a value when we create it.
2. Tell Swift what data type the variable will hold on later.

We’ve already seen (1) earlier, but for (2) we can use **type annotations**.

### Type Annotations

```swift
var instructor: String
instructor = "Vin"
```

<figure><img src="/files/dOH2CKAxk4dA6autdxu8" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
**As convention, we only put a space&#x20;*****after*****&#x20;the colon and not before!**
{% endhint %}

The key takeaway here is that Swift is a **statically typed** **language**, meaning that the type of every property, constant and variable that we declare needs to be specified at compile time. This is a good thing because it prevents us from putting anything inside of the “box”. This is known as **type safety**. Let’s demonstrate this by introducing a new data type **Int** (integer).

### Integer and Type Safety

```swift
var instructor: String
instructor = "Vin"

var year: Int
year = 2025
```

<figure><img src="/files/XaONhvEMXE4YFiapGrHJ" alt=""><figcaption></figcaption></figure>

Everything works fine, but what if we were to swap the values of `instructor` and `year`?

<figure><img src="/files/Dsq7UzAGmGdSxkhkA95o" alt=""><figcaption></figcaption></figure>

Our code no longer compiles because we tried assigning a value whose type does not match the type of the variable at the time it was created.

### Float and Double

We can store decimal numbers by using a **Float** and **Double**. The difference between these two is that a Double has twice the accuracy of a Float, and hence takes up more storage.

<figure><img src="/files/2CDUzDv95BW5q6GuOxZk" alt=""><figcaption></figcaption></figure>

### Boolean

A **Bool** (boolean) in Swift is a data type that can hold one of two values: `true` or `false`.

<figure><img src="/files/d1Gy5fd26TVPgwvzvfiY" alt=""><figcaption></figcaption></figure>

### Type Inferencing

Earlier when we assigned an initial value to a variable,

```swift
var instructor = "Vin"
```

Swift automatically **infers** what data type the variable will hold. This is known as **type inferencing**. We could also specify a data type and provide an initial value at the same time:

```swift
var instructor: String = "Vin"
```

Most of the time, we will not be using type annotations and prefer having Swift infer our types. However, there are times when type annotation should be used. This will come with practice and from seeing how we write our code.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://ios-course.cornellappdev.com/resources/textbook/swift-basics/data-types.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
