swift - 访问脚本 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,该应用程序是在构建阶段由脚本创建的。
解决方案
该文件位于主包文件夹中,而不是资源路径:
let fileManager = FileManager.default
let imagePath = Bundle.main.path(forResource: "fileName", ofType: "txt")
let content = try! String(contentsOfFile: imagePath)
推荐阅读
- sql - Is there a way to create contents of the schema into a Table in BigQuery?
- functional-programming - 非终止函数有类型吗?
- python - 我在为 vscode 的烧瓶环境中设置 libsass、pysass 和资产模块时遇到了一些问题
- python - 如何在python中计算文本文件中的最后一个字符
- php - Using url parameter in route to use in conditional
- css - Full width non responsive CSS for mobile only and only on Woocommerce single pages
- django - Django - 通过 Google API 转换经纬度地址并保存对象
- python - 检查多个正则表达式模式
- knapsack-problem - 如何重新包装多个最大容量的背包,将他们的物品倾倒在一堆,洗牌,并移除一些物品?
- c# - c# tabControl 背景颜色