使用 Swift 的 FileManager 遍历文件夹及其子文件夹中的文件 [英] Iterate through files in a folder and its subfolders using Swift's FileManager
本文介绍了使用 Swift 的 FileManager 遍历文件夹及其子文件夹中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我对 Swift 编程还很陌生,我正在尝试遍历文件夹中的文件.我查看了此处的答案,并尝试将其翻译为 Swift 语法,但没有成功.
I'm quite new to programming a Swift and I'm trying to iterate through the files in a folder. I took a look at the answer here and tried to translate it to Swift syntax, but didn't succeed.
let fileManager = NSFileManager.defaultManager()
let enumerator:NSDirectoryEnumerator = fileManager.enumeratorAtPath(folderPath)
for element in enumerator {
//do something
}
我得到的错误是:
Type 'NSDirectoryEnumerator' does not conform to protocol 'SequenceType'
我的目标是查看包含在主文件夹中的所有子文件夹和文件,并找到具有特定扩展名的所有文件,然后对它们进行处理.
My aim is to look at all the subfolders and files contained into the main folder and find all the files with a certain extension to then do something with them.
推荐答案
使用 nextObject()
enumerator
的方法:
while let element = enumerator?.nextObject() as? String {
if element.hasSuffix("ext") { // checks the extension
}
}
这篇关于使用 Swift 的 FileManager 遍历文件夹及其子文件夹中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文