Swift:遍历文件夹及其子文件夹中的文件 [英] Swift: iterate through files in a folder and its subfolders

查看:1068
本文介绍了Swift:遍历文件夹及其子文件夹中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个新的编程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.

任何帮助都非常感谢!

推荐答案

p>使用: $

while let element = enumerator?.nextObject() as? String {
    if element.hasSuffix("ext") { // checks the extension
    }
}

这篇关于Swift:遍历文件夹及其子文件夹中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆