Challenge 14, Why code doesnt work?


In Chris’s code, there are 3 images, which called fruit, fruit, fruit. Why they are not called apple cherry star?
And what is an actual problem in my code?? I was just following his instance, and I didnt make any mistake. If i have 2 Images like cherry + star or another couple, it works good. But when I add the third Image, it all goes wrong immediately.
Also I cant see my Images sometimes for some reason, if I recall them into “fruit” ( I added images in assets.xcassets)

The error Xcode gives you tells you exactly where the problem is. You have your string interpolation of slot3 outside the string instead of inside it.

This line:

Image("fruit" \(slot3))

should be:

Image("fruit\(slot3)")

In Chris’s code, there are 3 images, which called fruit, fruit, fruit. Why they are not called apple cherry star?

Because then the code would have to be more complicated. With the images named fruit1, fruit2, fruit3, references to them can be created by appending a random integer to the end of fruit.

But why I cannot see my freaking Images tho ??? xd
They are named, like they should be , or Im wrong ?


In Assets.xcassets they are named fruit1 fruit2 fruit3

Your fruits in the screenshot are named fruit11, fruit12 or fruit13, or fruit21, fruit22 or fruit 23, or fruit 31, fruit32 or fruit33.
That’s because the name is composed of fruit1 + the random integer. (And similar for fruit2 and fruit3)

You need to write
Image("fruit\(slot1)")
and similar.

Got u, thanks! Now it is working !

1 Like

Screen Shot 2021-08-11 at 11.59.33 AM

My code is not working and I would be grateful if someone could tell me why

Because your images are named fruit, fruit-1 and fruit-2 but the generated names in the code are fruit1, fruit2 and fruit3.

The names you generate in the code need to exactly match the names of the images in your assets.

I dont get what your saying. What do I need to change?

You need to change the names of your image assets to fruit1, fruit2 and fruit3.

When you click the Spin button, three variables are generated:

slot1 = Int.random(in: 1...3)
slot2 = Int.random(in: 1...3)
slot3 = Int.random(in: 1...3)

The numbers generated by these lines are appended to the word “fruit” when the images are displayed:

Image("fruit\(slot1)")

Image("fruit\(slot2)")

Image("fruit\(slot3)")

This gives the strings fruit1, fruit2 or fruit3, depending on what numbers are generated for the three slots. Your image asset names have to match those strings exactly in order for them to be found by your code and displayed.

1 Like

Screen Shot 2021-08-11 at 1.10.19 PM
It is still not working

Your asset naming is still the problem.

They should be fruit1, fruit2 and fruit3 respectively.

You have fruit-1, fruit-2 and fruit-3. Remove the hyphen in each case.

1 Like

Thanks