How do I count a number of images in a folder reference in swift

3.7k views Asked by At

I'm working on an custom emoji keyboard in Swift and I'm having trouble finding and counting the images in a folder reference called "emojis".

EDIT: To clarify my issue is that let contents always end up as nil.

The structure from the location of the .xcodeproj file looks like this: EmojiBoard/emojis/emoji-0.png and so on.

I've been trying to use the NSFileManager with no luck.

let fileManager = NSFileManager.defaultManager()
let contents = fileManager.contentsOfDirectoryAtPath("emojis", error: &error)
println(contents)

This prints nil. I've also tried "EmojiBoard/emojis" and "/EmojiBoard/emojis".

I need this to determine how many images there are and loop them all out without having to resort to an insane switch statement or something like that.

Thank you!

P.S. Please note that I'm coding in Swift, not Objective C. I'm not proficient enough to convert C programming to swift I'm afraid. D.S.

3

There are 3 answers

5
Leo Dabus On BEST ANSWER

if you created folder reference when adding the folder to your project use it like this (emojis folder icon is a blue folder):

let resourceURL = Bundle.main.resourceURL!.appendingPathComponent("emojis")
var resourcesContent: [URL] {
    (try? FileManager.default.contentsOfDirectory(at: resourceURL, includingPropertiesForKeys: nil)) ?? []
}
let emojiCount = resourcesContent.count
print(emojiCount)

if you created groups when adding the folder to your project use it like this (emojis folder icon is a yellow folder):

let resourceURL = Bundle.main.resourceURL!
let resourcesContent = (try? FileManager.default.contentsOfDirectory(at: resourceURL, includingPropertiesForKeys: nil)) ?? []
let emojiCount = resourcesContent.filter { $0.lastPathComponent.hasPrefix("emoji-") }.count
print(emojiCount)
1
Woodstock On

From the top of my head, without access to an IDE to test this code, I reckon something like this:

let fileManager = NSFileManager.defaultManager()


let contents = fileManager.contentsOfDirectoryAtPath(path, error: &error)


for var index = 0; index < contents.count; ++index {
    println("File is \(contents[index])")
}

If you replace 'path' above with your documents directory, this code should loop through the whole folder and print out all files.

If you just want the count of items just do this:

println("count is \(contents.count)")
2
Duncan C On

The problem (or at least a major part of the problem) is your path. You can't pass in a path that's just a filename. You need an absolute path to one of the sandboxed directories available to your app like the documents directory.

Your code might look like this:

let  documentsDir = NSSearchPathForDirectoriesInDomains(   
  NSSearchPathDirectory.DocumentDirectory,
  NSSearchPathDomainMask.UserDomainMask,
  true)[0] as! NSString
let emojisPath = documentsDir.stringByAppendingPathCompnent("emojis")
let contents = fileManager.contentsOfDirectoryAtPath(emojisPath, 
  error: &error)
println(contents)

(That would work if your emojis folder is in your app's documents folder. If instead your emojis are in your app bundle (built into the app) you would need to use different code entirely (using NSBundle functions to get a path to the directory inside the bundle).

EDIT:

If you want to find files in your app's bundle use the NSBundle method resourcePath, and then append the folder name to the bundle's resourcePath using stringByAppendingPathCompnent, like the code above.