|
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 |
TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts! 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 |
BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.
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.