使用 Swift Playgrounds 从用户路径读取文件

问题描述

我已使用 iPad 将文件下载到用户下载文件夹中,而不是 Playground [脚注] 的资源包中。

有没有办法使用 Playground 阅读它?我尝试了很多不同的东西,但似乎没有任何东西可以访问该文件

以下是对我不起作用的示例:

// Playground to analyze my finances downloaded from my bank
import SwiftUI;
import Foundation;

// Read in transactions
var filename = "filename.txt"

do {
    print("Letting dir")
    let dir = try! FileManager.default.url(for: .downloadsDirectory,in: .userDomainMask,appropriateFor: nil,create: false);
    print("Dir set,appending file")
    var fileurl = dir.appendingPathComponent(filename)
    
    print("Letting contents of file \( fileurl )");
    let contents = try String(contentsOf: fileurl,encoding: .utf8)
    
    // Process Transactions
    print(contents)
    
    // display results
    
}
catch {
    print("Error opening file: \( error )")
}

我认为问题在于它访问的是资源包的下载文件夹,而不是系统包,但我不知道如何解决这个问题,或者是否可以完成。

感谢您的帮助。这令人难以置信地令人沮丧,这是我项目的第一步。

大卫

[footnote] 我什至不知道如何访问资源包 - GUI 允许我做的只是编辑 Playground 代码。而且我了解资源路径经常更改,因此无论如何这似乎都不是正确的做法。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)