WWDC24 SALE: Save 50% on all my Swift books and bundles! >>

Add Collection Operations on Noncontiguous Elements

Available from Swift 6.0

Paul Hudson      @twostraws

SE-0270 introduces various new methods to handle more complex operations on collections, such as moving or remove multiple items that aren't contiguous.

This change is powered by a new type called RangeSet. If you've ever used IndexSet from Foundation, think of RangeSet as being IndexSet except for any kind of Comparable type rather than just integers.

Lots of Swift API has been upgraded to RangeSet. To give us some example data to work with, we could create an array of students with exam results like this:

struct ExamResult {
    var student: String
    var score: Int
}

let results = [
    ExamResult(student: "Eric Effiong", score: 95),
    ExamResult(student: "Maeve Wiley", score: 70),
    ExamResult(student: "Otis Milburn", score: 100)
]

We can get a RangeSet containing the indices of all students who score 85% or higher like this:

let topResults = results.indices { student in
    student.score >= 85
}

And if we wanted to get access to those students, we can use a new Collection subscript:

for result in results[topResults] {
    print("\(result.student) scored \(result.score)%")
}

This subscript returns another new type called DiscontiguousSlice, which is similar to Slice in that for performance reasons it refers to elements stored in a different collection, except the indices are discontiguous, meaning that they aren't necessarily adjacent in the collection.

The "set" part of the name is there because RangeSet supports a variety of functions that come from the SetAlgebra protocol, including union(), intersection(), and isSuperset(of:). This also means that inserting one range into another will merge any overlapping ranges rather than creating duplicates.

Save 50% in my WWDC sale.

SAVE 50% To celebrate WWDC24, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Other changes in Swift 6.0…

Download all Swift 6.0 changes as a playground Link to Swift 6.0 changes

Browse changes in all Swift versions

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.