John
John

Reputation: 695

Can't access all files in subfolder

I want to access all files (photos) in photos folder. But it throws an error. When I delete ("/photos") it access all context including p.list etc but I want to get all context in just photo folder.

let fm = FileManager.default
let path = Bundle.main.resourcePath!+("/photos")

let items = try! fm.contentsOfDirectory(atPath: path)

for item in items {
            if item.hasPrefix("nssl") {
                pictures.append(item)
            }
        }

screenshot

enter image description here

Upvotes: 1

Views: 274

Answers (1)

vadian
vadian

Reputation: 285160

In Xcode a yellow folder is a virtual group, not a real folder. Real folders are blue. To create a real folder drag a folder with images into the project navigator and select Create Folder References.

You can get all jpg files easily with

if let items = Bundle.main.urls(forResourcesWithExtension "jpg", subdirectory: "photos") {
   let nsslPictures = items.filter{ $0.lastPathComponent.hasPrefix("nssl") }
   pictures.append(contentsOf: nsslPictures)
}

Consider that nsslPictures is an array of URL, not String

Upvotes: 2

Related Questions