BSM

Apr 12 2020

Featuring:

HWS+

HWS+ is an all new service from Hacking With Swift. As of writing, it has 3 courses, which each have a few sections. Until WWDC, HackingWithSwift will publish a new section to one of the 3 courses every day, which afterwards will be every few days.
I can say, after watching all the videos that are released, that they are from really high quality(not only the resolution, also the content🙂!), and are really recommended if you want to level up in your swift and iOS development skills.
Once you are there, be sure to check out the other courses they have here


How to get the first element of an array that matches a certain predicate.

How not to do it.

When you want to find in a array the first object that contains a certain word, you can use this code:

let array = ["this is the first sentence", "this is the second sentence.", "this is the third sentence  in the array.", "you can also have a fourth one", "or a fifth one", "or maybe a sixth one"]

for sentence in array {
    if sentence.contains("array") {
        print("We got the sentence with the word "\array\": \(sentence)")
        break
    }
}

So, what we do here is we first loop over the array with a for in in loop, then we check if that object contains the word what we want, in our case “array”, and then we print that we got it. Of course, you can also apply your logic there. After we print the message out, we use a break statement so that the for in loop will stop.

As you can see, the code to do all that(without the array) is already 6 lines of code, and if you use that in your code base, it could get very lange and very hard to maintain.

Refactor

Luckily, Swift has something build in that is very handy for this operation: the firstIndex(where:) method. With that build in swift method, the 6 lines of code above get replaced with just 2 lines of code:

if let index = array.firstIndex(where: {$0.contains("array")}) {
    print("We got the sentence with the word "\array\":\(array[index]))
}

What happens here is that we define a variable, index and we assign it something special: array.firstIndex(where:) that method loops over the array until it finds the value you want to be found, and returns the index of that value.

The value you want to be found, is specified after the where: parameters where you specify a closure with the predicted, in our case $0.contains(“array”) ( learn more about closures:)

NOTE: you see that we use $0 ? That is the value of what the firstIndex(where:) method is currently checking at. Its swift shorthand code. In our case, we check if the value of the array at the index where at now, contains the string we specified.

Then, where inside the if statement, and we can use the index to print out something we want.

conclusion

firstIndex(where:) is a very handy method that lets us shorten our code a lot. In addition, you also have the method lastIndex(where:) which does the same, but it returns the index of the last matching predicate.

I hope this will help you maintain your code more easily and that it helped level up your swift skills.

Don’t forget that you can email me at questions@bdev-code.nl for any questions, feedback or if you just wanted to say hi.