How do I count a number of images in a folder refe

2019-07-09 09:15发布

问题:

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.

回答1:

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

let resourcePath = NSBundle.mainBundle().resourcePath!.stringByAppendingPathComponent("emojis")
var resourcesContent : [NSURL] {
    return NSFileManager().contentsOfDirectoryAtPath(resourcePath , error: nil)! as! [NSURL]
}
let emojiCount = resourcesContent.count
println(emojiCount)

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

let resourcePath = NSBundle.mainBundle().resourcePath!
let resourcesContent = NSFileManager().contentsOfDirectoryAtPath(resourcePath , error: nil)!
let emojiCount = NSFileManager().contentsOfDirectoryAtPath(resourcePath , error: nil)!.filter{ $0.lastPathComponent.hasPrefix("emoji-") }.count
println(emojiCount)


回答2:

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)")


回答3:

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.