|
Hello, When deleting items from an array in a ForEach we need to create a function like
which takes as argument something of type and then attached it to the ForEach as an argument to the
Something I am confused about though is where we are passing in our I've noticed other instances where there appears to be implicit arguments passed into functions like this but I have not fully grasped what the rule governing when I shold expect behavior like this. Thanks |
|
SwiftUI calls the function you provide in the |
|
I still don't get how this works, how does SwiftUI know to pass in those parameters? |
|
If you select the
or from Developer Documentation
.onDelete requires a parameter to be passed to it that takes an Calling .onDelete can result in a change, or not, in the view content (DynamicViewContent).
|
|
Because it knows how many rows you have in the list and which rows you have swiped to delete (or deleted in edit mode) and can pass in the index of those rows. So if you have, say, 10 rows and you swipe on row #5, SwiftUI will pass in an |
|
I feel your pain. One thing you might be stubbing your toe on: You may be thinking that ForEach is a swift command similiar to 'for each' or 'do while' in other languages like C++, JAVA, etc. It's not. Dig deeper! ForEach is a struct in SwiftUI. Developer Documentation: ForEach As you know, in SwiftUI structs can have properties, computed properties, enums, and functions. So the documentation states that .onDelete is a function within the ForEach struct. @greenamberred points out the function's signature. Back to your confusion. You still wonder how SwiftUI knows how to pass in an IndexSet into its .onDelete function. The documentation states that the ForEach struct creates views on demand. To do this, it must have some internal private var variables to keep track of the views it creates. They are private so you can't see them, nor can you change them. Yet, the ForEach struct keeps track of every view it creates, along with some other @State properties. So if you TAP on one or three rows, the ForEach struct will update its internal private vars. It's a pretty good guess to think that one of those private vars is a set of views that you have TAPPED. Also a good guess to assume these are stored as an IndexSet. So, if you then execute the ForEach function named .onDelete(), the ForEach struct obliges. It has an internal set of indices of selected views AND it has the closure you want to execute. As it executes the closure, it is updating its internal variables so that it can redraw the views on demand!
PS: This is one of the reasons why the id: \.self is a critical parameter in the ForEach struct's initializer. If your ForEach struct generates 15 views, and six of them are named "@twostraws", the .onDelete() method will have a very hard time trying to decide which one of those six views to delete. The id parameter in the struct's init() tells the ForEach struct what element makes each view unique. |
|
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.
Sponsor Hacking with Swift and reach the world's largest Swift community!
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.
Link copied to your pasteboard.