|
I have a Dice rolling game which I thought was working just fine. ContentView shows the dice as they roll. I have a diceRolls array that gets updated with the latest rolls. I have a DiceRollsListView that displays the list of rolls. All of that works. I have swipe to delete code, and some empty list code... all of that works...and the deletions do get saved to disk. However, when you roll the dice after a deletion...and go back to the list view... it shows all of the rolls that you last deleted. this ONLY happens after a dice roll. If you go the list BEFORE a dice roll, the list shows the deletions correct...close the app and reopen, it shows the deletions correct. So it is as if ContentView does NOT seem to realize the array was emptied or modified. The GitHub for this is: https://github.com/VulcanCCIT/DiceFun This has to be something simple and I am sure Paul covered what this might be somewhere in the course, but I am stumped. I must be missing something. Any help on this would be totally appreciated. I posted the code that I think you will need below, but it might be easier to just load the GitHub as it has it all. Thank you in advance!!! ContentView-ViewController code:
ContentView code:
DiceRollListVIew code:
|
|
@Obelix unless I have totally mis-understood your reply, and Paul's lessons on MVVM, I have made a ContentView-ViewController exactly how he does... "Right from the start, my head hurts seeing that you've defined the ViewModel class in an extension to your ContentView. Remember from the review above, the DataBox™ should exist outside your views so your view can all share the same DataBox™." This is exactly how Paul has shown us in various lessons. I do have a dice model, I just didnt post in in my question... Dice model:
Dice View:
So given these little code snipits I am not questioning your answer, but just trying to see where I have deviated from some of the other lessons... the FriendMinder is somewhat how i patterned the ViewModel-Controller... |
|
@Obelix, its part of the normal HackingWithSwift 100 Days of SwiftUI The FriendMinder is HackingWithSwift+ but my code is also similar to the BucketList lessons, at least as far as the ContentView and ContentView-ViewController ...at least I think it is. tomorrow I will look at your explinations and suggestions and compare to BucketList... Of Course, thank you for your time already invested in helping my UnSwiftyNess lol |
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! |
|
So close yet so far!. I bet that what you feeling at the moments, we all do! The reason is that you are creating two instances of the I would look at a This will then leave only code for PS looks good (maybe bit need on animation of dice) PSS It might be look good to do this and if you save the color of dice could use that even better (maybe just have "Total"). |
|
@NigelGee yes I remember some DataController lessons, I need to go back and look at those. Your explination makes sense and yes, as soon as I found that bug I went from feeling Jubliant to Depressed in a matter of minutes!!! lol. it was like climbing a huge mountain and just before I got to the top, I fell back down the hill! I LOVE the idea of putting the dice in the List View! I am saving the color as an AppStorage right now... Question, what did you mean here: "PS looks good (maybe bit need on animation of dice)" I am now off to look up DataControllers. |
|
I never said use CoreData, I said make a DataController - this can be used to save/read/delete to FileManager! You now made a Have you update your Github. I will check PS I did not mean all the dice to same color what they use to roll the dice that was saved. EG if user used on first time blue then next time yellow etc then those color show in the list. Means you have to add a color property to get the color used at time and save it. |
|
@NigelGee you must of read my reply just before I deleted it...I talked about CoreData for a bit, then I had some other questions on my code then I figured it out and deleted my post. I have not updated GitHub yet as I did, just for grins try CoreData but I am not sure it works well with the MVVM model I already had in place. So I am going to back out of that... Once I do that I will have a ViewModel controller for both ContentView and DiceRollListView, but coding the central location to save is eluding me. What I tried still has the same issues syncing....but I think maybe im a lot closer. I hope it is slow at my work tomorrow and I can do some more coding...once I have it updated ill let you know. I do appreciate your assistance and all of this is helping me learn. I thank you a ton for that! :) Coding is a hobby and a passion and not my real job. My goal is to someday, get something into the appStore ... I have only been doing Swift for a year... prior to that I did some C# , C, c++ and Delphi, but all of those just off and on. My real job is a Radio Broadcast engineer :D So hang tight and I hope to have something tomorrow :) |
|
@NigelGee, actually, while it all was fresh in my mind, I pulled out the CoreData stuff and its back to the addition of the DiceRollListView-ViewController Now it just needs some sort of DataController. Its about 12:20 am Central Time in the states, so off to bed for now :). Not sure what timezone you are in but have a look at my Github :D |
|
Hi I live in UK (FYI). I have put this on GitHub - DiceFun-main I ended up calling the "dataController"
I then started to move code from Content View Model to a new file called
Now inject to enviroment in
Then in
And also used it
This now read/write the dice rolls correctly. |
|
@NigelGee Check it out! I added a var lastDiceColor: String to the DiceData.swift. Then in UpdateRolls, I update the last rolled color :) Thank you SOOOO much for your help!!! If you ever got to Memphis, Tennessee where there is fantastic BBQ it will be ribs on me! or a Steak!! lol Again The GitHub for this is: https://github.com/VulcanCCIT/DiceFun I gave you credit in the push description and left your //Created by NigelGee comments in as you are of course the creator of not only these changes, but creator of great code! I was telling my wife that in a lot of posts here, you seem to help tons of people! This forum is lucky and blessed to have your help sir!!! Thank you again! |
|
Look great, more dice like!
Now you have the
Add a new
then change
Then in
|
|
@NigelGee, Oh I love that as I love the ternary operator! I will add that tomorrow, when I get into the office. I remember thinking in the back of my head, when I saw all of my variables, "I bet there is a way to simplify this". I was telling my wife just yesterday that software is never quite finished! Thank you again sir!! So very thankful! <3 |
|
@NigelGee, I had to modify DiceView slightly from your code...mainly I had to cast Color( in the .shadow line and to change the preview code.
then ContentView a small change:
basically removing the for: Not sure if I did that right...but it works what do you think? |
|
|
|
@NigelGee, I understand I was just seeing if I fixed what you suggested correctly, as it was not compiling...specifically the Cast of Color... I just edited my post as I re-read your reply about init... NO WONDER it was not compiling...I didnt put the init in there! OOOOPS Now it works just as you had it. My apologies...I just had missed that and I do like how that works! AGAIN, THANK YOU SO MUCH! I have so much more to study...you help has been immense! I do most of this during off hours as my real job is not coding... I just love it so I do it when I have time... |
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!
You need to create an account or log in to reply.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.