NEW: Start my new Ultimate Portfolio App course with a free Hacking with Swift+ trial! >>

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 Fernando Olivares

SPONSORED Would you describe yourself as knowledgeable, but struggling when you have to come up with your own code? Fernando Olivares has a new book containing iOS rules you can immediately apply to your coding habits to see dramatic improvements, while also teaching applied programming fundamentals seen in refactored code from published apps.

Try the book!

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.