|
Ok, I know this is basic. I am working with Beta 6 of XCode 12 I have the following struct:
I then have a SwiftUI View that has a list. The rows are defined by the following view. When I user taps on the plus button it adds the hymn to the userChosenHymns array and changes the plus button to a minus button. I have it adding to the array but I cant seem to get it to remove that chosen hymn from the array. I'm having trouble getting the index of the where that hymn resides in the array. Can someone help me with this?
Thanks, Mark |
|
Try using |
|
Instead of trying to find an index to remove, you could use |
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! |
|
|
|
@roosterboy, can you give me an example because ive tried that as well. i cant seem to get it to work. its either that or i dont know how closures work. |
|
@roosterboy, I did some research on closures. I'm stuck at this point, this is what I came up with but its not working,
I get the following error:
Not sure what I'm doing wrong. I'm stuck at this point. Can you help me with this? Thanks Mark |
|
Ok, @roosterboy. I think I figured it out. I needed to tell the where portion the arguments I needed to pass to the removeAll. This is what I wrote and it seems to be working.
Am i correct in assuming that removeAll is taking the jm: JSONModel and returning everything else that doesn't match that id. I assume that jm is being passed in when I wrote return jm.id == hymn.id. |
|
Hey @appledad05, wondering if you can help me with a similar issue - I have a similar model setup where I want to allow a user to remove a plant (from a ForEach list of plants) when they click on the settings button. Originally, I had it setup with a list to where users could easily swipe and delete. This was the function that was performed with .onDelete(perform: deleteReminder) for the original list:
Now, I want it to trigger through an ActionSheet popup, and I'm not sure how to access the specific ID/index of the plant with each card in the ForEach. Any suggestions on how to call the above function outside of the ForEach list? |
|
@appledad05, you could write it like this, using trailing closure syntax and implicit return:
IMO, it looks a little neater and is easier to read.
It's not really returning anything; it's modifying |
|
Hi @aabeshde , Im new to iOS and haven't worked with action sheets. However, I would assume that if you have some sort of array/collection then when you display the action sheet and press the delete button you can use removeAll on the array and use a trailing closure to filter the array in place. This will remove the plant in question and return everything else back in the same array. In my case I was building an array of hymns that go with a playlist and if the user chose a hymn it added it to the playlist, but I wanted to give them the ability to unselect the hymn, which in turn would remove it from userChosenHymns array. So doing something like the following worked for me:
id is a unique identifier for each hymn. So you will just need something unique to filter on. I hope that helped. If I can anwser anything else please let me know. Appledad05 |
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.