UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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.

Simon

7      

Hi,

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

8      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.