从Documents Directory Swift iOS的子文件夹中获取文件列表

里迪沙

我正在使用此代码从文档目录获取PDF文件列表。现在,我想从文档目录中名为“ MyFiles”的文件夹中查找PDF文件列表(如果存在)。

我怎样才能做到这一点??

func listFilesFromDocumentsFolder() -> [String]
{
    var theError = NSErrorPointer()
    let dirs = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.AllDomainsMask, true) as? [String]

    if dirs != nil {
        let dir = dirs![0]
        let fileList = NSFileManager.defaultManager().contentsOfDirectoryAtPath(dir, error: theError) as! [String]
        var count = fileList.count
        for var i = 0; i < count; i++
        {
            var filePath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first as! String
            filePath = filePath.stringByAppendingPathComponent(fileList[i])
            let properties = [NSURLLocalizedNameKey, NSURLCreationDateKey, NSURLContentModificationDateKey, NSURLLocalizedTypeDescriptionKey]
            var attr = NSFileManager.defaultManager().attributesOfItemAtPath(filePath, error: NSErrorPointer())
        }
        return fileList.filter{ $0.pathExtension == "pdf" }.map{ $0.lastPathComponent } as [String]
    }else{
        let fileList = [""]
        return fileList
    }
}

提前致谢..

里迪沙

这是工作代码。

func listFilesFromDocumentsFolder() -> [String]
{
    var theError = NSErrorPointer()
    let dirs = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.AllDomainsMask, true) as? [String]

    if dirs != nil {
        let dir = dirs![0]//this path upto document directory

        //this will give you the path to MyFiles
        let MyFilesPath = dir.stringByAppendingPathComponent("/BioData")
        if !NSFileManager.defaultManager().fileExistsAtPath(MyFilesPath) {
            NSFileManager.defaultManager().createDirectoryAtPath(MyFilesPath, withIntermediateDirectories: false, attributes: nil, error: theError)
        } else {
            println("not creted or exist")
        }

        let fileList = NSFileManager.defaultManager().contentsOfDirectoryAtPath(MyFilesPath, error: theError) as! [String]

        var count = fileList.count
        for var i = 0; i < count; i++
        {
            var filePath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first as! String
            filePath = filePath.stringByAppendingPathComponent(fileList[i])
            let properties = [NSURLLocalizedNameKey, NSURLCreationDateKey, NSURLContentModificationDateKey, NSURLLocalizedTypeDescriptionKey]
            var attr = NSFileManager.defaultManager().attributesOfItemAtPath(filePath, error: NSErrorPointer())
        }
        println("fileList: \(fileList)")
        return fileList.filter{ $0.pathExtension == "pdf" }.map{ $0.lastPathComponent } as [String]
    }else{
        let fileList = [""]
        return fileList
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

从iOS错误的Documents文件夹内的文件夹保存并获取图像

来自分类Dev

在Powershell中获取用户的Documents文件夹

来自分类Dev

在iOS的documents文件夹中创建多个目录

来自分类Dev

Windows Documents文件夹中不存在的文件

来自分类Dev

Lightroom中〜/ Documents中的dynamiclinkmediaserver文件夹

来自分类Dev

Dropbox而不是Documents文件夹?

来自分类Dev

大型Documents文件夹的性能影响

来自分类Dev

Dropbox而不是Documents文件夹?

来自分类Dev

如何将高清图像从Alasset保存到iOS中的Documents文件夹

来自分类Dev

在Swift中创建文件夹

来自分类Dev

似乎我的文本文件没有永久存储在Documents Directory中

来自分类Dev

当Directory.Move子文件夹和父文件夹时,IOException访问被拒绝

来自分类Dev

Swift-如何获取文件夹内的文件路径

来自分类Dev

在Swift iOS中根据大小对文档目录中的文件和文件夹进行排序

来自分类Dev

在iOS应用程序的/ Documents /文件夹中提供初始文件

来自分类Dev

如何在xcode项目中获取文件夹目录?iOS Swift

来自分类Dev

如何在xcode项目中获取文件夹目录?iOS Swift

来自分类Dev

将本地生成的PDF保存到iOS8中的应用程序的documents文件夹中

来自分类Dev

Swift:如何从文件夹中读取文件

来自分类Dev

如何编译给定文件夹中的任何 swift 文件?

来自分类Dev

从 Directory.GetDirectories() 中包含或排除特定文件夹

来自分类Dev

尝试使用.pth文件在Mac上的documents文件夹中添加路径

来自分类Dev

尝试使用.pth文件在Mac上的documents文件夹中添加路径

来自分类Dev

在ios Swift中的Images.xcassets文件夹中以集合视图显示图像

来自分类Dev

Windows命令提示符-获取重定位的用户的Documents文件夹

来自分类Dev

React Native iOS从应用程序的Documents文件夹读取图像

来自分类Dev

ios swift3中的自定义文件夹加密

来自分类Dev

Windows 10 Documents文件夹的环境变量

来自分类Dev

Documents 文件夹有 7 个硬链接

Related 相关文章

  1. 1

    从iOS错误的Documents文件夹内的文件夹保存并获取图像

  2. 2

    在Powershell中获取用户的Documents文件夹

  3. 3

    在iOS的documents文件夹中创建多个目录

  4. 4

    Windows Documents文件夹中不存在的文件

  5. 5

    Lightroom中〜/ Documents中的dynamiclinkmediaserver文件夹

  6. 6

    Dropbox而不是Documents文件夹?

  7. 7

    大型Documents文件夹的性能影响

  8. 8

    Dropbox而不是Documents文件夹?

  9. 9

    如何将高清图像从Alasset保存到iOS中的Documents文件夹

  10. 10

    在Swift中创建文件夹

  11. 11

    似乎我的文本文件没有永久存储在Documents Directory中

  12. 12

    当Directory.Move子文件夹和父文件夹时,IOException访问被拒绝

  13. 13

    Swift-如何获取文件夹内的文件路径

  14. 14

    在Swift iOS中根据大小对文档目录中的文件和文件夹进行排序

  15. 15

    在iOS应用程序的/ Documents /文件夹中提供初始文件

  16. 16

    如何在xcode项目中获取文件夹目录?iOS Swift

  17. 17

    如何在xcode项目中获取文件夹目录?iOS Swift

  18. 18

    将本地生成的PDF保存到iOS8中的应用程序的documents文件夹中

  19. 19

    Swift:如何从文件夹中读取文件

  20. 20

    如何编译给定文件夹中的任何 swift 文件?

  21. 21

    从 Directory.GetDirectories() 中包含或排除特定文件夹

  22. 22

    尝试使用.pth文件在Mac上的documents文件夹中添加路径

  23. 23

    尝试使用.pth文件在Mac上的documents文件夹中添加路径

  24. 24

    在ios Swift中的Images.xcassets文件夹中以集合视图显示图像

  25. 25

    Windows命令提示符-获取重定位的用户的Documents文件夹

  26. 26

    React Native iOS从应用程序的Documents文件夹读取图像

  27. 27

    ios swift3中的自定义文件夹加密

  28. 28

    Windows 10 Documents文件夹的环境变量

  29. 29

    Documents 文件夹有 7 个硬链接

热门标签

归档