Skip to main content

One-sided ranges

About 1 minSwiftArticle(s)bloghackingwithswift.comswiftswift-4.0

One-sided ranges 관련

HACKING WITH SWIFT

What's new in Swift?

One-sided ranges | Changes in Swift 4.0

One-sided ranges

Available from Swift 4.0

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 (apple/swift-evolution)open in new window.

Other Changes in Swift 4.0
Encoding and decoding data using Codable | Changes in Swift 4.0

Encoding and decoding data using Codable
Multi-line string literals | Changes in Swift 4.0

Multi-line string literals
Improved keypaths for key-value coding | Changes in Swift 4.0

Improved keypaths for key-value coding
Improved dictionary functionality | Changes in Swift 4.0

Improved dictionary functionality
Strings are collections again | Changes in Swift 4.0

Strings are collections again

Download Swift 4.0 playgroundopen in new window


이찬희 (MarkiiimarK)
Never Stop Learning.