Arrays in Swift
Tags: swift, ios, mobile, tutorial
Published: 2022-05-12 10:31:10

Till now, what you saw is assigning only one value to a variable/constant. You might have one question though, what if I have a bunch of values to store?

Answer to that question is quite straight forward, to store the collection of Hot wheels, we keep it in a rectangle or square box. Just like that, if you want to store a collection of items in one variable, an array helps there. Arrays can be defined as a collection of same type of values in an ordered position. Let's first have a look at the syntax,

```swift
var arraySyntax = [1, 2, 3, 4, 5, 6]
```

Here, after declaring and naming a variable, you first open a square bracket and then store a value in it. Now, add multiple items by separating them up with a comma, and once you are done adding your values, close it with a closing square bracket.

The values stored are an Int data type. Swift automatically recognizes and assigns the data type after adding the first value, which means you can't add a String or Double value to your variable or a constant later.

Let's access/bring an item out of an array, but you must know that a number is allotted for its position whenever you have a new element in your Array. We don't call it a number, but an index and a group are called indices.

```swift
arraySyntax[0]
arraySyntax[1]

```

When changing a specific item of your collection, we can do just like swapping a value of a single variable.

```swift
arraySyntax[2] = 10
arraySyntax[3] = 75
```

Almost every programming language out there will start their count from 0, Swift is one of them. So, when you put an index value **1** when referring to an Array item and getting it printed, you will be surprised by the output. Since the result displayed will be **2** instead of **1**, we start with 0 to refer to the first element.

We discussed earlier in this article that Swift automatically recognizes a data type. Now, here is one thing we would like you to try:

Copy the code from the snippet and print it,

```swift
var team = ["Tim Cook", "Craig Federighi", "Eddy Cue", "Jony Ive", "Krishna Babani", 6]
```

The Xcode or Playgrounds App won't print anything; instead, it shows an error **Heterogeneous collection literal could only be inferred to
"[Any]'; add explicit type annotation if this is intentional" and a solution "Insert " as [Any]"**

Tapping on fix will solve your error and print an Integer as well. One in a million would try storing an item different from the stored ones, and for that, you will need to set your variable type to **Any**.

```swift
var team: [Any] = ["Tim Cook", "Craig Federighi", "Eddy Cue", "Jony Ive", "Krishna Babani", 6]
```

This also means you can set a data type to String, Int, or Double.

```swift
var team: [String] = ["Tim Cook", "Craig Federighi", "Eddy Cue", "Jony Ive", "Krishna Babani"]
```

Here we will need to drop an Integer from our array/collection.

## How to create an Array?

The next important thing to remember is that the square brackets for arrays cannot be left empty. (There is a twist)

For example,

```swift
var friends = []
```

Hence, this results in an error **Empty collection literal requires an explicit type**

To solve your question: "How can we just create an array and store the values later ?"

The answer is simple, specify what type of data you will store in your Array. There will be a slight change in Syntax:

```swift
var friends: [String] = []
// Can now keep your Array empty.
```

The other notable Syntax which also means the same:

```swift
var friends: [String]()
```

## Array Operators

We can merge or combine two arrays but must be a little careful when doing it.

To combine two arrays, rules of adding an item to your Array will also apply here. This means that both the arrays must hold the same data type, or else you might get the same or similar error that we saw above.

```swift
var teamApple = ["Craig", "Tim", "Greg", "Eddy", "Jeff"] // List 1
var teamSA = ["Krishna", "Mayank", "Krish", "Amit", "Raj"] // List 2
```

Assume that Apple hosts an in-person WWDC 2023. Someone at Apple got a task to create two lists and send the invites to people from List 1 and rest later after a week. In this case, the team of Apple executives and our team. 

The demo invites are sent out to the people from List 1. Once they confirm, the guy then sends out an invite to us (SA Team).

But now, if he/she wants to know who is coming to this event, he will be combining these lists into a single list.

```swift
var membersAttending = teamApple + teamSA
// You won't see an error as both the variables hold the same type of data.
```

That's all for the concept of Arrays. Keep yourself engaged in this learning process because Determination and Consistency are the only keys to success. We encourage you to learn more related topics next. Till then,

Eat. Sleep. Swift Anytime. Repeat