How do you find an item in an array in Swift? Let’s find out! In this tutorial you’ll learn how to use the various generic functions to find matching items in an array.
Arrays are fast, easy to implement, and even better to use in Swift, thanks to the convinces that Swift affords developers. The Swift documentation for arrays is a great resource for. Swift 4 Programming #6 - Arrays - Duration: 8:01. TheCodex 11,347 views. IOS Swift Making a Calculator (Converting Int to String and String to Int) - Duration: 7:34.
We’ll get into:
Ready? Let’s go.
Before we dive into Swift’s ways you can find items in an array, let’s find out how such an algorithm actually works.
This will definitely help your understanding of algorithms and iOS development in general. When you’re learning iOS development, it’s a great idea to set aside some time to learn how commonly used algorithms work.
The challenge is simple: given an array of an arbitrary number of items, find the index of a given value. So, if we have an array of names, your job is to find the index for a particular name. Where’s the name in the array?
Here’s our approach at a glance:
Here’s what that looks like in Swift code:
This is what happens at the top of the above code:
Then, we’re using a for loop to loop over every name in the names array, with for name in names { ···. Inside the loop, this happens:
Quite simple, right? The code just loops over every item in the array until it finds what we’re looking for.
The time complexity of the above algorithm is O(n), or linear time, because in the worst case scenario we’re looping over every n items once to find what we’re looking for. The time needed to search the array increases linearly with the number of items in the array. This is about as good as it’s going to get for a collection that doesn’t use a hashmap. If you need a test membership in O(1), check out Set.
We can actually save ourselves some trouble by slightly changing the for loop. And lets turn the code into a Swift function, too.
In the above function we’re using the enumerated() function to get a sequence of index-value pairs. This way we can directly get the index of a particular value, without needing to keep track of the current index ourselves.
And the function directly returns an index when it has found a value, which will stop the loop. When no value could be found, the function returns nil. As such, the return type of the function is Int?. (And note that we’re using function argument labels, too.)
You’d use the function like this:
Awesome! Now that we know how the algorithm works, let’s see what Swift functions we can use to find an item in an array.
Sign up for my iOS development course, and learn how to build great iOS 14 apps with Swift 5 and Xcode 12.
The easiest approach to find an item in an array is with the firstIndex(of:) function. Here’s how:
You call the firstIndex(of:) function on the array you want to search. This function is a generic, so it can be used regardless of the array’s type.
Let’s say we’re tired of Zaphod, and we want to replace him with someone else. Here’s how:
In the above code, the index of the value 'Zaphod' is first searched for in the names array. When found, that index is used to replace the value with another string. (Although I have no idea why you’d want a Vogon on your team, unless you like bureaucracy, highways and/or poetry.)
Because the firstIndex(of:) function searches an array from front to back, it’ll always return the index of the first match it finds. If you want to get the last match in an array, you can use the lastIndex(of:) function.
What if you don’t know exactly what you’re looking for? That’s where the firstIndex(where:) function comes in. This function takes a closure, a so-called predicate, as a parameter to search the array for matches.
Backgrounds 3 0 – dynamic desktop wallpapers free. Here’s an example:
In the above code we’re working with a grades array of integers. Due 1 1 – set reminders in your own words. We want to know the index of the first array item that’s below 7. We don’t know the exact number we’re looking for, only that it needs to be less than 7.
That’s what we use firstIndex(where: { $0 < 7 }) for. The code between the squiggly brackets is a closure. This closure is executed for every item of grades, and we’ve found a successful match when it returns true.
Let’s break that down:
And we’re using optional binding to get the non-optional value from firstIndex(where:) if it doesn’t return nil.
The function firstIndex(where:) belongs to a group of functions that can select elements from an array that match a given predicate, including:
Keep in mind that if you want to use the above functions, the items in your array must conform to the Equatable protocol. Types that conform to this protocol can be compared with the operator, which is used to check if two items are equal.
And your code gets pretty cool, pretty quickly from there. You can use any kind of comparison in the where closure. Things like:
Let’s move on!
Thunderstruck free online. The syntax for functions like firstIndex(where:) is similar to Swift’s higher-order functionsmap(_:), reduce(_:_:) and filter(_:).
What if you want to find all items in an array that match a given predicate? The Swift Standard Library does not yet have a simple function for that, but we can of course make our own!
Let’s have a look. Here’s the all(where:) function:
The all(where:) function is an extension for the Array type, with a generic type constraint that says that this extension only applies for array elements that conform to the Equatable protocol. In other words, you can only use all(where:) for array items that can be compared with the operator – just like the other firstIndex(where:) Wild casino games. functions.
The all(where:) function declaration defines a predicate parameter of closure type (Element) -> Bool, and it returns an array of type [Element]. This is exactly the same as the other functions.
Inside the function, we’re using a function called compactMap(_:). This function essentially calls a closure on every item in an array (i.e., “map”) and also removes a value when it is nil (i.e., “filter”).
The expression predicate($0) ? $0 : nil will return the value $0 when the predicate closure returns true, and nil when it returns false. Differently said, the closure will only let values pass for which predicate($0) is true, i.e. only array items that match the given condition are returned. And that’s exactly what we want!
Here’s how you use it:
Awesome!
Sign up for my iOS development course, and learn how to build great iOS 14 apps with Swift 5 and Xcode 12.
The Swift programming language lets us do programmatic somersaults, cartwheels and saltos – with just a few lines of code. In this tutorial, we’ve begun to understand how these algorithms work, and then applied them to find different bits of data in arrays. Great work!
Want to learn more? Check out these resources: