Learn Courses My Dashboard

Fatal error, index is out of range

hi everyone, I have finally run into a problem that I can’t just take a screen shot off and ask for help.
I don’t really know where to go, do you guys have any suggestions about how best to show you my project so maybe someone can tell me what I’m doing wrong?
I would post sections of the code, but its across many different pages because I got spaghetti code all over the joint. I would really appreciate some help with my problem but also how best to share my problem?

thanks again team!

You could compress the entire project (at the root folder level) into a zip file and post it to either DropBox or Google Drive and then create a share link and paste that in a reply.

OR

If you are familiar with GitHub then push the code to a repo (make sure that it is public) and paste a link in a reply.

hi Chris, im not sure I have done it right, its my fist time using GitHub, but here is the link

the problem I am having is on the second tab, when press the plus to open each individual edit window, and then press the trash can to delete that card from that array.
again im sorry for all the spaghetti code all over the place. I hope you can make sense of it

thanks again!

Yes, I have I have successfully downloaded the project and it builds. I’ll have a look at your issue as soon as I get the chance.

hey Chris! thanks again! I really appreciate it! if I could buy you a coffee I would!

Hi Andrew,

Sorry mate but I’m struggling to decipher what you are trying to do.

No worries Chris,
So what I am trying to make is an app to meet track of planned visits and dates for a long-distance relationship,
So far I have the visits tab kind of working
You can add new planned visits and edit ones that already exist,
The trouble comes when I try to delete a visit from the visits array with a button in the edit screen of each individual visit.
I have the button see to do something like

 modle.alltrips.remove(at: index ?? 0)

sorry it you have wasted a bunch of time trying to figure this out

ezgif-6-51e2bc734f1b

editView.swift, line 26:

let editThis = modle.allTrips[index]

Once you delete the trip item, index is no longer valid, yet you still try to use it to load the trip into your edit view.

You need to rethink how you are accessing objects in your views; too much reliance on directly indexing into an array and things like index ?? 0. Try using more bindings and passing the objects themselves.

that’s good to know!
I tried that at first, but then had trouble editing each one individually, so that was the solution I came up with.
I actually thought it might be that but haven’t figure out how to fix it, I thought that closing the view before the object is removed from the array it might fix it but it didn’t,
I think the solution I came up with was kind of a bandaid solution which is causing this problem, I think I’m gunna walk back a few steps to try to pass the information around correctly like you have suggested, thank you for helping out!

ok, so its fixed!
thank you for the reply, I feel so uselesss having baked my head against the wall for like three days, and I should have just tried to do it properly the first time instead of trying to find a workaround.
I think the original problem I was having was that for my original save button I had it try to used the same UUID and it didn’t like that. so like I said I just kind of created a work around for that problem and created 10 more.
but I have gone and passed the trip through in a more sensible way and it seems to be working!

thank you @roosterboy and thank you @Chris_Parker for all you help!

1 Like