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

SOLVED: Milestone Project: 1-3

Forums > 100 Days of Swift

This may be super trivial, but I'm just getting very frustrated with this.

Problem: I cannot print out the name of the flag files provided to us where I'm unable to getting anything printed onto the console.

What I did:

I dragged the country image files into our Assets.xcassets catalog.

This is my code in ViewController.swift

let fm = FileManager.default
let path = Bundle.main.resourcePath!
let items = try! fm.contentsOfDirectory(atPath: path)

        for item in items {
            if item.hasSuffix("png"){
                print(item)
            }
        }

If I simply print the files without item.hasSuffix("png") I get this:

  • _CodeSignature
  • Base.lproj
  • Assets.car
  • Milestone Project 1-3
  • Info.plist
  • PkgInfo

Any help would be appreciated, thanks!

4      

Hi! You cannot access files from Assets like this. The alternative is to create new group in your project and move your files there. But for images using Asset Catalog should be the preferred solution.

5      

Thank you both, I have spent hours on this... I thought I was insane cause for Project 1 we loaded images into a folder and accessed via the Bundle.main and Project 2 we used Assets.xassets and the challange says "Load the list of available flags from the app bundle. You can type them directly into the code if you want, but it’s preferable not to."

and we're using the flags so I figured, they are all @2x and @3x so it would be a waste to just load them otherwise...

so I guess we just choose one or the other instead of trying to tell the app which to use. hmmm this feels off but I'm so new everything feels off...

4      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

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.