14 days Challenge (Lesson 13)

Good afternoon!

After working on the challenge, I got everything displayed but needed help to make it work properly. So I looked at the solution and tried to fill in my missing parts. Everything works at it should except that the various assets are not displayed or visible in any way. The score changes but the “fruits” are not visible.
Are there any particular place I should look to have them appear? I am puzzled.
How come the names of each slots (apple, cherries, star) are not written anywhere on the proposed solution. Instead, the names “slot1, slot2 and slot3” are used? Do I need to change the names of my assets to match? I tried that but did not resolve the issue!
Please provide me with some guidance.
Thanks!

What Chris did in his solution was to change the names of the assets to fruit1, fruit2 and fruit3 rather that have apple, cherry and star and used string interpolation in the Image(…) statement to get the asset image.

slot1, slot2 and slot3 are the random numbers generated when you tap the button.
For example the first Image statement is Image("fruit\(slot1)")
If the variable slot1 had the value 1 it means that the image selected from the asset would be named fruit1

Thank you! I was able to fix my problem to get the the slots showing again.

1 Like

I too have a problem with the Lesson 13 Challenge. I can see and understand the solution posted here but I thought the challenge was to use the assets as named i.e. apple, cherry, star. So my ‘solution’ tried to find how to add numbers to these string names so as to be able to assign a randomised image. I came up with the solution in the attached image. BUT although I had no errors nor did Xcode complain the images stopped appearing. I believe the logic is correct but obviously the coding for that logic is not. I would be grateful if you could point out where my coding has gone adrift. (I haven’t finished the tidying up yet). Thank you
John Christopher Wise

@Chris_Wise

Welcome to the Code Crew community.

If you were going to use the names of the assets as they are then the other way to do this is to add them into an array like:

@State private var symbols = ["apple", "cherry", "star"]

Then have an array of integers which can be used to represent the position of the items in the HStack as viewed from left to right which when assigned random numbers can be used to index the items in the symbols array.

@State private var position = [0,0,0]

In your Button action you could have:

position[0] = Int.random(in: 0...2)
position[1] = Int.random(in: 0...2)
position[2] = Int.random(in: 0...2)

Essentially you are assigning a random number to each of the positions and that value can be used as an index to the symbols array.

In your HStack where you display the images, have this:

Image(symbols[position[0]])
Image(symbols[position[1]])
Image(symbols[position[2]])

I hope that does not confuse the heck out of you.

1 Like

Many thanks Chris. I never thought of using an Array as I was ‘in the mindset’ of the 14 day challenge series. (Before I discovered ‘Coding with Chris’ I had worked my way through the ‘Swift Playgrounds’ from the App Store which did cover Arrays). I will play around more with this challenge using your advice.
Just to set my mind at rest: was my attempted solution faulty in logic or coding (or both!). And if in coding why did Xcode not complain? I simply ask so as to tie up loose ends in my thinking as I spent some time on my failure - though I did learn a great deal else!
p.s. I do think Coding with Chris is brilliantly structured and presented and I look forward to the next phase.
Thanks again.

Your logic was flawed insofar as in your series of @State declarations for card1 2 and 3 and spinner1 2 and 3 it looked like your intentions was to assign card1 to spinner1 and card2 to spinner2 etc.
In actual fact all you were succeeding in doing was to assign spinner1 2 and 3 to a string in each case.

When you use spinner1 inside of Image() it will contain a string of either “card1”, “card2”, or “card3” so you are trying to find an image of that name which does not exist.

Some time down the track you will come back to this exercise and realise what you were doing was not logical even if at the moment it may seem that it looks reasonalble

Many thanks Chris. I do appreciate your help with this.
In light of your answer I have one final question (promise): Is there any way within Xcode that I can view the current contents/value of a particular selected variable or constant. If there is it would be invaluable in debugging my own code as I could instantly see errors of the above sort?
Many thanks again.

This is a method you can use to print variables from within the SwiftUI views.

Create a new Swift file in your project (maybe call it View-Extension) and add the following:

import SwiftUI

extension View {
    func Print(_ vars: Any...) -> some View {
        for v in vars { print(v) }
        return EmptyView()
    }
}

Inside your view where you want to print variables, all you do is place the following:

Print(varOne, varTwo....)

You can add text too so like this:

Print("Inside HStack", varOne, varTwo, ......)

Thank you Chris. You have been a great help.
Kind Regards
John Chris Wise

Chris, I have done as you suggested but cannot find where the Print Statement outputs to. It does not appear to output to the canvas nor can I see an output panel at the bottom of the screen as there is in the ‘Playgrounds’. And the debug panel in place of the Project Navigator panel shows nothing either. Maybe I am getting ahead of myself in asking these questions?
Thanks again

To make the Debug Console visible, press the keyboard combination Command + Shift + C or from the Xcode menu select View > Debug Area > Activate Console.

That works a treat. Many thanks

Hi Chris_Parker, in the provided solution for the Slots-Challenge in Lesson 13, the images were not renamed by Chris but are in an image sets called fruit1, fruit2, fruit3. See attachment. How do we create these image sets and get the images into them? thanks, Roger

In the “Before You Begin” Section of the Module, there is a Resources “lesson” in which there is a link to “All resources, projects and code”
Select that and you will be presented with a DropBox Folder structure. Tap on Lesson 13 and then tap the Slot Assets.zip file, then select “Download” to download the assets from the Dropbox folder.

Open the zip file and then drag all the images into your Assets folder. You can rename them to the way Chris Ching did so that each group is fruit1, fruit2 and fruit3 or you can leave the names as apple, cherry and star but you will have to think about how you would use them.

Ha, it didn’t dawn on me that we can rename assets after they’re dragged into the Assets folder. Thanks.

ok so I didn’t know anything about Array thing since ive only done the 14 day challenge so far. and I didn’t want to cheat and just change the asset name. so I actually just created a function:

func identifyFruit(_ fruitNum:Int) → String{
var fruit:String = “watever”
if fruitNum == 1{
fruit = “apple”
}
if fruitNum == 2{
fruit = “cherry”
}
if fruitNum == 3{
fruit = “star”
}
return fruit
}

hope this helps. im new to this so I don’t know how to share it cool like the other people