|
So I am deviating from the challenge slightly and only dealing with 2 dice, both dice 6 sided. I want a list view that I can call up from ContentView once a button is pushed that shows the running totals of all dice rolls.... so I have a file called DiceData.swift with this code inside:
ContentView is handling the display of the dice as they are rolled, animation of the dice, and keeping track of the total dice roll on each roll (for example dice1's roll is 3, dice2's roll is 5, lastRowTotal = 8) So if I rolled once and got the 8 rolled a second time and got a 3 I want a list view that would show: Dice 1 roll: 3. Dice 2 roll 5 Total for roll 1: 8 Dice 1 roll: 6. Dice 2 roll 1 Total for roll 1: 7 ...etc I cant seem to get a list to work. I would think by Day 95 I would know how to do a list!! I do know how but I guess having that struct in one file and the DiceRollView in a another file and ContentView running the show I have confused myself and I have been looking at it toooo long lol Any help would be awesome! |
|
Hi @VulcanCCTT Here's a basic idea of using a list with your RollResult type. Hope this helps jog your memory.
I'm not that far in the course yet, but I thought I'd play around with the whole dice thing, lol. I started with a type that represents a virtual die, with a specified number of sides and a random roll value.
Another struct that represents a snapshot or a record of dice and their values after a roll.
Now a controller class that holds a history of dice rolls and their values as we roll the dice
Quick demo view showing usage.
Hope some of this stuff helps. Have a great week. |
|
To create a list view in SwiftUI that displays the running totals of all dice rolls, you can follow these steps: Create a SwiftUI view for your list. You can create a new view called DiceRollListView.swift. Here's an example of how it might look:
In your ContentView, you should create a @State variable to store the list of RollResult objects and pass it to the DiceRollListView. You may also need to add a button to trigger the display of the list. Here's an example of how you can modify your ContentView:
This code assumes that you have the dice rolling logic in your ContentView. When you press the "Show Roll List" button, it will present the DiceRollListView in a sheet, displaying the running totals of all dice rolls. Make sure to adapt and integrate this code into your existing project. The RollResult struct should remain in DiceData.swift. This structure keeps your code organized and follows the principles of SwiftUI's view separation.# [Medical Billers Texas] |
SPONSORED Alex is the iOS & Mac developer’s ultimate AI assistant. It integrates with Xcode, offering a best-in-class Swift coding agent. Generate modern SwiftUI from images. Fast-apply suggestions from Claude 3.5 Sonnet, o3-mini, and DeepSeek R1. Autofix Swift 6 errors and warnings. And so much more. Start your 7-day free trial today! Sponsor Hacking with Swift and reach the world's largest Swift community! |
|
@amyjackson wow, how simple! I tend to open a rabbit hole and fill it with code! lol. @KitchenCrazy I will probably modify Amy's solution but move to the Model, View, ViewModel similar to what you are doing and pattern it after some of the other HWS projects I have done where I used that. I will post a github of this when i finish it all! Thank you to both!!! |
|
@amyjackson wow, how simple! I tend to open a rabbit hole and fill it with code! lol. @KitchenCrazy I will probably modify Amy's solution but move to the Model, View, ViewModel similar to what you are doing and pattern it after some of the other HWS projects I have done where I used that. I will post a github of this when i finish it all! Thank you to both!!! @amyjackson, I think I am close but the DiceRollListView is not showing any data ...just a blank row.... I know diceData has data as I printed it after a roll and the console shows: [DiceFun.RollResult(id: 33CBE0B6-94FC-42BC-B527-3AEB502D289D, lastDice1RollValue: 6, lastDice2RollValue: 1, lastRollTotal: 7, allRolls: [])] In ContentView I have a timer handling all of the logic... and this is where I am populating diceData
so I am not sure why the DiceRollListView is blank... instead of var diceRolls: [RollResult] in DiceRollListView shouldnt it be some sort of @StateObject? otherwise isnt diceRolls a local struct local to JUST DiceRollListView and not a variable being populated over in ContentView? |
|
oh disregard... I had two lists going... a List inside of a List (as I had a list already in my DiceRollListView then as I was "porting" over Amy's code I put that List inside of the other... took out the top list and all is well! |
|
@amyjackson and @KitchenCrazy with your help, I have completed this challenge. I need to test haptics tomorrow but I am going to mark this topic as resolved. I still want to clean my code up and make it conform to MVVM, but I will work on that at another time. If you folks are on twitter, follow me @chuckcondron and I will post a github of this when I am done if you want to see it... even if you do not want to see it, feel free to follow me. Thank you both again!! |
|
Certainly, I can help you with basic CoreData problems in your training app. CoreData is a valuable framework for managing data in iOS applications, and I can assist with troubleshooting and offering guidance. Regarding the keyword "online shop Justelegance.de," you can find a diverse range of products on this online store, catering to various needs and preferences. Justelegance.de offers a convenient shopping experience for those seeking quality items and fashion-related products. |
|
|
|
@Vulcan was rolling dice and code and got lost in some of the logic.
I think you nailed it about where you confused yourself. The numerous requirements you add to your applications to make them feature rich can easily overwhelm your initial designs. When I first peeked at your code, my initial thoughts were your code was good, but your organization needed clarification. I shared the How to Eat an Elephant idea with another Swift coder facing similar problems. So the question might be, how can you break your elephant sized problem down into bite sized pieces? RefactoringYou've probably already moved on. But if you have a chance, paste this code into a new project. Take a look how I broke the problem down into smaller and smaller bite-sized pieces. Model One DieFirst, I modelled a single Die. It only knows that it can have a face value. It also has one thing it can do, roll itself and produce a random face.
Model a Pair of DiceNow that you have a working die, grab two of them and make a pair.
Assemble the ViewNow that you have the business logic for rolling two dice, build the interface that your user will see. Here you'll want to maintain a collection of all the previous rolls the user tossed. Notice how the objects in this view just call the business logic defined in the structs above? The business logic of rolling, counting, displaying is built into the Here you just want to grab a new pair, roll them, then add them to your history collection.
Keep coding!Hope this helps you refactor your code by keeping business logic separated from your display logic. |
|
Certainly! It looks like you want to display a list of dice rolls with their corresponding values and totals. Here's an example of how you can structure your SwiftUI views to achieve this: First, update your DiceData.swift file:
Now, in your ContentView.swift, make sure you have an instance of DiceRolls:
This assumes you have a Button for rolling the dice and updates the list accordingly. Make sure to replace the placeholder values with your actual dice rolling logic. This should help you display a list of dice rolls in your SwiftUI app. |
|
Certainly! It looks like you want to display a list of dice rolls with their corresponding values and totals. Here's an example of how you can structure your SwiftUI views to achieve this: First, update your DiceData.swift file:
Now, in your ContentView.swift, make sure you have an instance of DiceRolls:
This assumes you have a Button for rolling the dice and updates the list accordingly. Make sure to replace the placeholder values with your actual dice rolling logic. This should help you display a list of dice rolls in your SwiftUI app. i am sure your code will work. #solved www.google.com |
SPONSORED Alex is the iOS & Mac developer’s ultimate AI assistant. It integrates with Xcode, offering a best-in-class Swift coding agent. Generate modern SwiftUI from images. Fast-apply suggestions from Claude 3.5 Sonnet, o3-mini, and DeepSeek R1. Autofix Swift 6 errors and warnings. And so much more. Start your 7-day free trial today!
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.