Last but not least, Swift 4 introduced Python-like one-sided collection slicing, where the missing side is automatically inferred to be the start or end of the collection. This has no effect on existing code because it's a new use for the existing operator, so you don't need to worry about potential breakage.
Here's an example:
let characters = ["Dr Horrible", "Captain Hammer", "Penny", "Bad Horse", "Moist"]
let bigParts = characters[..<3]
let smallParts = characters[3...]
print(bigParts)
print(smallParts)
That code will print out ["Dr Horrible", "Captain Hammer", "Penny"]
then ["Bad Horse", "Moist"]
.
For more information see the Swift Evolution proposal for this new feature.
SPONSORED Get accurate app localizations in minutes using AI. Choose your languages & receive translations for 40+ markets!
Sponsor Hacking with Swift and reach the world's largest Swift community!
Download all Swift 4.0 changes as a playground Link to Swift 4.0 changes
Link copied to your pasteboard.