UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Day 19: Not sure how UIImage is using an array of strings to pull images

Forums > 100 Days of Swift

We have this code for the flag game project:

func askQuestion() {
      button1.setImage(UIImage(named: countries[0]), for: .normal)
      button2.setImage(UIImage(named: countries[1]), for: .normal)
      button3.setImage(UIImage(named: countries[2]), for: .normal)
  }

My confusion is this: according to the code, it seems button1 is "setting its image" to the output of UIImage's "named: countries[0]"

However, countries[0] is simply a string. How is the system getting an image out of it? Is it checking the assets for an image whose title matches the string in countries[0]?

Additionally, if I added a random string into countries like "Zebras", would UIImage be able to pull an image out of it as well even though it doesn't exist?

Trying to get a conceptual grasp of this code...

3      

How is the system getting an image out of it? Is it checking the assets for an image whose title matches the string in countries[0]?

Yep.

Additionally, if I added a random string into countries like "Zebras", would UIImage be able to pull an image out of it as well even though it doesn't exist?

Nope. How would the system create an image if it doesn't exist in your assets?

4      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

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.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.