gpt4 book ai didi

Swift3 不写入文件

转载 作者:行者123 更新时间:2023-11-28 09:26:08 25 4
gpt4 key购买 nike

在下面的代码中,我试图写入我在 xcode 项目中的文件。但对于某些问题,它没有写信给它。我的文件中没有更新的文本。(JSON 文件)

        responseString = "Some Text"           
if let path = Bundle.main.path(forResource: "mapsdatademo", ofType: "json") {
do{
try responseString.write(toFile: path, atomically: false, encoding: String.Encoding.utf8)
}catch {"Error writting data"}
}

我可以通过以下方式读取文件:

if let path = Bundle.main.path(forResource: "mapsdatademo", ofType: "json") {
do {
let data = try Data(contentsOf: URL(fileURLWithPath: path), options: .alwaysMapped)}

注意:我正在尝试获取 json 数据并将其写入文件。

最佳答案

iOS 是沙盒化的。您可以从包中读取但不能写入。写入文档目录。

if var path = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
path = path.appendingPathComponent("mapsdatademo.json")
do{
try responseString.write(toFile: path, atomically: false, encoding: String.Encoding.utf8)
}catch {"Error writting data"}
}

关于Swift3 不写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40231581/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com