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

SOLVED: Not understanding thoroughfully onDelete()

Forums > 100 Days of SwiftUI

i dont understand that modifier. heres the code from day 36 that i have the issue with:

func removeRows(at offsets: IndexSet) {
    numbers.remove(atOffsets: offsets)
} 

1) why do we use "at" in parameters? isnt it just an external name? 2) what is atOffsets?

heres the second code that i dont understand

ForEach(numbers, id: \.self) {
    Text("Row \($0)")
}
.onDelete(perform: removeRows)

the onDelete modifier, how does it know we're deleting a specific part of our list?

also not relevant to swift, does anyone know why my keyboard sometimes type letters twice? same with spacebar. Macbook pro.

2      

1) why do we use "at" in parameters? isnt it just an external name?

Yes, "at" is just an external name here. The only reason for using it is so that when we call the function, it can be read more naturally.

var unwantedIndexes = IndexSet()

//This can be read easily and a person can figure out what it does pretty well as they read it
removeRows(at: unwantedIndexes)

//This could be more confusing to read and understand
removeRows(offsets: unwantedIndexes)

2) what is atOffsets?

atOffsets is of type IndexSet which means that it is basically a Set of Int values.

When we are removing a single row, the IndexSet only has one Int in it, and that Int represents the index (position in the array or collection) that you want to remove.

However, if we want to delete multiple items at once, an IndexSet containing all of the indexes (positions in the array or collection) that we want to be removed would be used.

3) the onDelete modifier, how does it know we're deleting a specific part of our list?

When you use onDelete(perform:) the perform parameter must be of the type (IndexSet) -> Void (A function that takes an IndexSet as a parameter and returns Void) and Swift will automatically provide the IndexSet to the perform function as a parameter, based on how the user has interacted with the interface.

3      

heres the second code that i dont understand

ForEach(numbers, id: \.self) {
    Text("Row \($0)")
}
.onDelete(perform: removeRows)

the onDelete modifier, how does it know we're deleting a specific part of our list?

SwiftUI knows which rows you are deleting based on the user actions and passes those to whatever you put in the onDelete handler.

also not relevant to swift, does anyone know why my keyboard sometimes type letters twice? same with spacebar. Macbook pro.

You have dirt or something under the keys. Happens to me all the time. Right now I'm mexperiencing it with the m key. See back there? Normally I would correct that but I'm leaving it in for illustration.

3      

Both answers are awesome! Thank you very much!

2      

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.