这个问题:如何获取资产目录组中的所有图像名称?
我想访问在 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)
和许多其他组合.
这不是家庭作业,我已经尝试了各种解决方案,但由于我不确定文件在文件系统中的位置(如何在应用程序中访问根目录(,我似乎无法找到该文件。
问题:如何从名为challenger的应用程序读取./Challenge/fileNames.txt,该应用程序由构建阶段的脚本创建。
该文件位于主捆绑包文件夹中,而不是资源路径中:
let fileManager = FileManager.default
let imagePath = Bundle.main.path(forResource: "fileName", ofType: "txt")
let content = try! String(contentsOfFile: imagePath)