如何快速计算文件夹引用中的图像数量

如何快速计算文件夹引用中的图像数量

本文介绍了如何快速计算文件夹引用中的图像数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Swift中的自定义表情符号键盘,但是在名为表情符号"的文件夹引用中查找和计数图像时遇到了麻烦.

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

澄清我的问题是让内容始终以nil结尾.

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

.xcodeproj文件所在位置的结构如下所示:EmojiBoard/emojis/emoji-0.png等.

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

我一直在尝试使用NSFileManager,但是没有运气.

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

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

这将显示nil.我也尝试过"EmojiBoard/表情符号"和"/EmojiBoard/表情符号".

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

我需要它来确定有多少图像并将它们全部循环出去,而不必诉诸疯狂的switch语句之类的东西.

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.

谢谢!

P.S.请注意,我是在Swift中编写代码,而不是在Objective C中编写.恐怕我不够熟练,无法将C编程转换为Swift.D.S.

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.

推荐答案

如果在将文件夹添加到项目中时创建了文件夹引用,请像这样使用它(表情符号文件夹图标是蓝色文件夹):

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)

这篇关于如何快速计算文件夹引用中的图像数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-27 20:41