Well, Me from yesterday, I’m glad you asked and weren’t stumped for several hours.
One way is to use .contains()
.
This can tell you if the value exists in the array by returning a boolean value (true or false).
Another way to see if a value is in an array, but also get the specific value out of an array like you want, is to use .indexOf()
.
This will return nil
if the value searched for is not in the array, or will return the index number of the value in question. If you just want to check it’s there or not, then contains()
is the way to go, but if you definitely want the Value, then it’s just one small step to get the value once you’ve got the index number.
So, let’s say we have an array like so:
var fruitArray: [String] = ["Apple", "Banana", "Kiwi"]
We could test to see if the value “Apple” appears in the string. .contains()
uses dot notation, where you put the value you’re looking for in parenthesis after it.
fruitArray.contains("Apple")
And .indexOf()
works the same, like so:
fruitArray.indexOf("Apple")
So if we just want to do something simple like get a true/false response for "if the value is in the array", we can use .contains()
:
var fruitArray: [String] = ["Apple", "Banana", "Kiwi"]
fruitArray.contains("Apple") //returns true
Of course this doesn’t allow us to actually get the value itself though - if we try to print it for example, we’ll only get true
or false
.
var fruitArray: [String] = ["Apple", "Banana", "Kiwi"]
print (fruitArray.contains("Apple"))
//prints true
This is where .indexOf()
comes in handy instead. With indexOf()
you return the index number of the array, rather than a boolean true/false. So you can do what you like with the array’s index number you receive. Like showing its value. We can do that in the same way we would in any other array - using array[index number]
:
var fruitArray: [String] = ["Apple", "Banana", "Kiwi"]
if let fruitIndex = fruitArray.indexOf("Banana") {
print(fruitArray[fruitIndex])
}
//prints ‘Banana’
Or we can do something like this, where we can use .contains()
to search two different arrays for the same value:
var fruitArray: [String] = ["Apple", "Banana", "Kiwi"]
var berryArray: [String] = ["Blueberry", "Strawberry", "Banana"]
var sneakyFruit = "Banana"
if fruitArray.contains(sneakyFruit) && berryArray.contains(sneakyFruit) {
print(sneakyFruit)
}
//prints ‘Banana’
You can use .contains()
in a function, like this easy one for checking if the entered value is in the array:
func fruitExists (name: String) -> Bool {
return fruitArray.contains(name)
}
fruitExists("Kiwi") //‘true’
fruitExists("Rhubarb") //‘false’
Or use a function with like this one using both - .contains()
to check the value exists and .indexOf()
to then edit it:
func shopListEdit (name: String) {
if fruitArray.contains(name) {
let fruitIndex = fruitArray.indexOf(name)
fruitArray[fruitIndex!] = ("\(name) and Custard")
}
}
shopListEdit("Banana")
fruitArray //["Apple", "Banana and Custard", "Kiwi"]
shopListEdit("Rhubarb")
fruitArray //["Apple", "Banana and Custard", "Kiwi"]
There you go, Me from yesterday, persevere and remember that no question’s too stupid to deserve an answer.