Swift 4.2 Essentials

Arrays

Your browser needs to be JavaScript capable to view this video

Try reloading this page, or reviewing your browser settings

The second type of Collections in Swift is Arrays. In the video, we will explore how to create and manipulate these types in your code lines.

Keywords

  • Swift 4
  • Ios
  • Tutorial
  • sets
  • arrays
  • dictionaries

About this video

Author(s)
Mark Hoath
First online
17 November 2018
DOI
https://doi.org/10.1007/978-1-4842-4232-2_8
Online ISBN
978-1-4842-4232-2
Publisher
Apress
Copyright information
© Mark Hoath 2019

Video Transcript

Now, just as you’re unlikely to use sets you are extremely likely to use arrays. So, arrays store values of the same type in an ordered list. So, arrays are immutable, that is, they can change when they’re created, but once they are assigned they are immutable is they can’t change. So, let’s have a look at how we define an array. So, we create a variable called summonce, and we use the bracer sign to indicate that there’s an array, and then we put the data type on the inside, and then to initialize it we pass an initializer. So, that is going to have the empty array. It’s going to have zero elements, and if we want to add some elements. So, we have summonce dot append, and unit element of type into which is a three, and if we want to do create an empty array then of course we’ve got this removal function just like we did with sets, and we can also simply pass an empty array, and just like with sets this is still going to be an array of integers even though it’s empty we can’t change the data type once it has been assigned.

So, let’s have a look at SUM default values. So, we’ve got an array of three doubles equals, and we can use this, pass an array repeating 0.0 count three, and then we can have another three doubles equals array repeating, and this time we’ll pass 2.53 times and then we will have arithmetic. So, we can go 6 doubles is equal to 3 doubles plus another 3 doubles. So, here we’ve got the number 0.0 that we’re going that’s repeated three times, and this time we’re going to repeat 2.5 times, and we will repeat the value 2.5, and we’re going to repeat it three times on the count. So, we have six doubles, and we’re going to go 0, 0, 0, 2.5, 2.5, 2.5. We can also create an array with literals, and that’s like what we did earlier. So, we’ve got a shopping list, and that is going to be an array of strings, and then we’ll put the array sign in, and just comma separate what we want. So, we’ll get milk, and eggs, and the other thing we can do is we can define the array by inferral, or by inference depending however you wanna say it. So, you’ll just go by shopping list equals an array of mile, no, of milk and eggs.

So, and we’ve redefined it, the shopping list, too. Thank you compiler. So, there’s a couple of functions you can use. There’s a fast way of figuring out how many elements are in the array, which is the dot count, and the other way of fast is is it empty? So, we can go shopping list dot count. That’s also how many items there are, and shopping list dot is empty. Now is empty is going to return true or false, and it’s going to go is the shopping list empty, and then you just say no, and then is the account equal to zero? That’s more efficient to use than empty, and it’s more natural reading, and Swift is all about, or one of the things it’s about, is about naturability of source code. The first item in array you can index in. So, you can go first item just like in c or objective c the first item is a zero item. So, let the first item equal shopping list, and then you can just pass that item. In the first item there’s going to be a string because we’ve got an array of strings. So string would be the third in this case, and then when we’ve got our shopping list we know we’ve got the depends, but we can also go plus equals, and then we can pass what we have to pass an array because operators only work with the same dial type, but we can pass an array with one item, milks, eggs, and bread. So that’s acceptable but passing that is not acceptable. There it is. It’s a binary operator, and it says we can’t mix a string in an array of strings. So, we need to pass an array, and you can do other things with indexes as well. You can insert shopping list from positions four to six, and we can change that to be apples and oranges, and essentially what that would do is it would take the fourth, fifth, and sixth items. So, everything between four and six, and we’d remove them, and replace them with just these two items, apples, and oranges. Then if you want to insert items quite often, or you want to insert not necessarily have to start or at the end or want to do it in some sort of order.

So, you can do shopping list dot insert after or you can insert before. So you can say insert after position to, and there’s of course the equivalent remove at the position to, and again these are using the index numbers where zero is the first item, and there’s also, if you want to get the first item, there is a fix and last item that you can also use method functions rather than having to traverse the entire list to get to the end, and just like you could in set you’re much more likely to do it, but we’ll come to it when we do full loops. You can do full item in shopping list, and just print item, and then basically what this is doing is it’s going through every item in your shopping list, and putting this tempered variable, and allow us to print it out each time. So, it allows us to pass through the entire array as needed.