firstIndex(of:) is here to stay.
SE-0204 added the lastIndex(of:)
and lastIndex(where:)
methods to the Swift standard library, while also renaming index(of:)
and index(where:)
to be firstIndex(of:)
and firstIndex(where:)
to avoid confusion.
However, even though the first…
and last…
methods were added as planned in Swift 4.2, index(of:)
seems to have snuck under the radar and was left intact and untouched – until now, that is.
As of Swift 5.0, index(of:)
and index(where:)
have been formally deprecated, which means they will be removed fully at some future point in time. Past experience has shown that Swift is serious about deprecation: you can expect warnings to appear as soon as you upgrade to Xcode 10.2, and for this method to go away completely in the next 12 months or so.
So, get ahead of the game: if you search for “.index” in your code to spot any places where you might be using the old method calls, and look to replace them with the equivalent “firstIndex” call. The functionality is identical, to the point where index(of:)
is actually implemented like this:
public func index(of element: Element) -> Index? {
return firstIndex(of: element)
}
I’m busy going through my original Hacking with Swift book as part of my 100 Days of Swift initiative, and I’m on the hunt for index(…)
calls. With around 6000 pages of Swift tutorials it might take a little time, but I’ll get there…
SPONSORED Play is the first native iOS design tool created for designers and engineers. You can install Play for iOS and iPad today and sign up to check out the Beta of our macOS app with SwiftUI code export. We're also hiring engineers!
Sponsor Hacking with Swift and reach the world's largest Swift community!
Paul Hudson is the creator of Hacking with Swift, the most comprehensive series of Swift books in the world. He's also the editor of Swift Developer News, the maintainer of the Swift Knowledge Base, and a speaker at Swift events around the world. If you're curious you can learn more here.
Link copied to your pasteboard.