【问题标题】:How to get the plist file lists in project如何获取项目中的plist文件列表
【发布时间】:2017-05-23 04:57:07
【问题描述】:

我有一些数据文件,例如

AAA.plist
BBB.plist
CCC.plist

我想获取这些 plist 文件的列表。 如果我事先知道文件名, 我可以得到这样的文件。

 let path : String = Bundle.main.path(forResource: "AAA", ofType: "plist")!
 var qplist = NSArray(contentsOfFile: path)

但是我想在事先不知道文件名的情况下获取plist文件列表。

【问题讨论】:

    标签: arrays swift plist


    【解决方案1】:

    您可以为此使用urls(forResourcesWithExtension:subdirectory:)

    if let urls = Bundle.main.urls(forResourcesWithExtension: "plist", subdirectory: nil) {
    
         print(urls)
    }
    //Set subdirectory with specific name instead of nil if files inside some directory.
    

    您也可以使用paths(forResourcesOfType:inDirectory:) 来获取路径数组。

    【讨论】:

    • 仅供参考 - 请注意,如果您的 Xcode 项目中的文件夹是蓝色而不是黄色,文件只会位于子目录中。
    • 是的,whitebear @rmaddy 正确设置 subdirectory 仅当文件夹颜色为蓝色而不是黄色时。
    • 感谢 Nirav D 并感谢 rmaddy 的建议。 Xcode 组/文件夹系统让我很困惑....
    • @whitebear 欢迎朋友 :)
    • 它适用于子目录,但是可以使用组而不是子目录吗???我想选择某个组下的 plist 文件
    【解决方案2】:

    使用 FileManager 中的枚举器:

    let path = Bundle.main.resourcePath
    let man = FileManager.default
    
    for file in man.enumerator(atPath: path!)! {
        print(file)
    }
    

    【讨论】:

    • 看看所有那些崩溃操作符。
    • 是的,是的,没有包括我关于需要零保护的正常免责声明:)
    • Bundle.urls(forResources...) 无论如何是一个更好的选择。
    猜你喜欢
    • 1970-01-01
    • 2013-10-03
    • 2020-09-11
    • 1970-01-01
    • 1970-01-01
    • 2012-09-22
    • 2019-11-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多