首页 > 解决方案 > 如何将数据文件与 macOS 终端应用程序捆绑在一起?

问题描述

我正在尝试将文本文件与我正在构建的 macOS 命令行工具捆绑在一起。我已将文件添加到项目中,但似乎无法从代码中访问它。我的项目如下所示:

项目资源管理器的图像,small_names.txt 与 main.swift 相邻

我用来尝试加载数据的函数如下所示:

func loadFile() -> [String] {
    let fileName = "small_names"
    guard let url = Bundle.main.url(forResource: fileName, withExtension: "txt") else {
        print("couldn't open \(fileName)")
        return []
    }
    // load that URL
}

然而,url似乎总是nil。当我运行程序时,我会couldn't open small_names打印出来。

我需要做什么才能将这个文件与我的程序一起发送?如何从代码中获取其 url?

谢谢。

标签: swiftxcodemacosterminal

解决方案


推荐阅读