首页 > 解决方案 > 访问脚本 Swift 创建的根文件

问题描述

从这个问题:如何获取资产目录组中的所有图像名称?

我想访问在 Swift 应用程序的根目录中创建的文件。

我的应用程序称为挑战,我的脚本是

# Type a script or drag a script file from your workspace to insert its path.
#!/bin/sh
>./Challenge/fileNames.txt
for FILE in ./Challenge/Images.xcassets/actions/*; do
echo $FILE >> ./Challenge/fileNames.txt
done

我努力了

    let fileManager = FileManager.default
    let imagePath = Bundle.main.resourcePath! + "fileNames.txt"
    let imageNames = try! fileManager.contentsOfDirectory(atPath: imagePath)

和许多其他的组合。

这不是家庭作业,我尝试了各种解决方案,但由于我不确定文件在文件系统中的位置(如何在应用程序中访问根目录),我似乎无法找到该文件。

问题:如何从名为challenge 的应用程序中读取./Challenge/fileNames.txt,该应用程序是在构建阶段由脚本创建的。

标签: swift

解决方案


该文件位于主包文件夹中,而不是资源路径:

let fileManager = FileManager.default
let imagePath = Bundle.main.path(forResource: "fileName", ofType: "txt")
let content = try! String(contentsOfFile: imagePath)

推荐阅读