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 Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge.
Sponsor Hacking with Swift and reach the world's largest Swift community!
Link copied to your pasteboard.