NEW: Subscribe to Hacking with Swift+ and accelerate your learning! >>

SOLVED: IndexSet explained

Forums > Swift

Evening folks,

Is someone able to provide, or pinpoint me in the direction of, a clear simple understanding of IndexSets? Whilst I've read the Apple documentation, I find reading 2-3 different articles helps to cement my understanding around different topics. Unfortunaterly with IndexSets the internet is not forthcoming with information.

My thirst for knowledge re IndexSets comes from day 7 (iExpense) of the SwiftUI course - what's the link between IndexSets and the items array? How does the IndexSet know where in the item array to remove an ExpenseItem? Does the swiped cell row # get passed to the IndexSet that then removes that # from the array? Or have I got it all wrong?

Any help would be mich appreicated.




I think one of the simplest way to think about IndexSet is that it is a sort of optimized array of integers that can store individual ones and also ranges. Say you want to store indexes 43 to 120. With traditional Array you need 77 elements to store all the indexes (120-43) but IndexSet is smart enough to store the start and end index so instead of 77 elements it can get away with just two.

However I would not think about this much. IndexSet is used very sporadically. The only usecase I can think of on the top of my head is reloading table view sections where I am just constructing it with single integer because the API expects IndexSet :D


Hacking with Swift is sponsored by Paw

SPONSORED Use Paw to build, test and describe web APIs. Paw has a lightning fast native macOS interface to compose requests, collaborate in real-time on API specs, and generate client code for your applications. You can import and export API definitions.

Discover Paw for Mac

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

Unknown user

Not logged in

Log in

Link copied to your pasteboard.