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

SOLVED: Question regarding Project 2 - "Guess the Flag"

Forums > 100 Days of SwiftUI

I've just finished this project and was wondering if someone could take a quick moment to explain to me how the images that we imported get "linked" to the "countries" array. I noticed that if I change "Estonia" to "Estoniar" for instance, the button for Estonia no longer exists, and the title at the top changes to "Estoniar". But, how did these images get linked to the array in the first place? Does Image(self.countries[number]) correlate the strings in the array with the filename of the images automagically? Thanks!

3      

You imported images into your assets catalog with names like estonia@2x.png, estonia@3x.png, france@2x.png, etc. Xcode creates an imageset from them that can be referred to by the simple name before the @ symbol, so estonia, france, etc.

When you use code like button1.setImage(UIImage(named: countries[0]), for: .normal), UIKit (or AppKit on the Mac) takes the string stored in the countries array at index 0 (say, e.g., "estonia") and uses that name to match an imageset in the assets catalog. It selects among the @2x, @3x, etc versions based on the device and its screen resolution.

So your code uses a string stored in the countries array to kick this all off and UIKit takes that string and does its thing behind the scenes to find a corresponding asset to display.

Make sense?

3      

yes, that does make sense. I had a feeling something like that was happening, but I couldn't quite put together how it was all working. Thanks for taking the time to explain it!

3      

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.