> 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-structures.md).

# Data Structures

We learned how to use variables and constants to store data, but only explored basic values such as integer numbers and text. However, when we program, we often need to hold more complicated data that requires a specialized format for organizing and retrieving the data. To do this, we use **Data Structures**.

### Arrays

The most common data structure that we will be using is an **array**. Arrays store a group of values together into a single collection, and we can access these values using their position in the array.

```swift
var staff = ["Vin", "Richie", "Tiffany", "Jennifer", "Antoinette", "Elvis"]
staff[0]
staff[1]
staff[2]
```

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

We use square brackets `[]` to mark the start and end point of the array and use commas `,` to separate each value.

Swift uses ***type inferencing*** to determine the type of `staff`. Because all of the elements inside of the array are strings, Swift knows that `staff` is an array of strings (`Array<String>`). If we change the value of an element to a different type, our code will not compile.

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

Instead of letting Swift infer what types our array will hold, we can specify the type that we want.

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

As we can see, if we put in a value that does not match with the given type, our code will not compile.

However, it is possible to allow our arrays to hold ***any*** type. We can give it the special `Any` data type:

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

When adding values to our array, we must first initialize it with an original value. The following code will not compile:

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

We can initialize our array in the following ways:

```swift
var staff: [String] = []
var staff = [String]()
```

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

Notice that we used an `append` method to add elements to the end of the array. Swift provides many methods that we can use on our array, and we can even add our own! We can also use operators such as `+` to glue arrays together and return a new array. Read more about them in the [Apple Documentation](https://developer.apple.com/documentation/swift/array).

### Dictionaries

Another common data structure that we might encounter are called **dictionaries**. These are similar to arrays except we use a **key** to access a value in the collection. In other words, dictionaries store **key-value pairs**.

```swift
var staffAge: [String: Int] = ["Vin": 19, "Richie": 20, "Antoinette": 4]
```

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

It is also common to break up our dictionary like so to keep things readable:

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

Similar to arrays, Swift provides a lot of methods that we can use with dictionaries. The [Apple Documentation](https://developer.apple.com/documentation/swift/dictionary) provides more information about them.


---

# 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-structures.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.
