iOS:如何写入项目中特定目录下的文件?

时间:2022-09-06 00:25:20

I'm trying to output some strings I've gathered in my app to a text file.

我正在尝试将我在应用程序中收集的一些字符串输出到文本文件中。

The text files will always exist in my project, like so:

文本文件将始终存在于我的项目中,如下所示:

I'm trying to overwrite whatever is in the file with a new (multi-lined) string:

我试图用新的(多行)字符串覆盖文件中的任何内容:

func writeFile(){

let theFile: FileHandle? = FileHandle(forWritingAtPath: "./MyFiles/file.txt")

if theFile != nil { // This is always nil!!!
    let data = ("Some text\nline 2\nline3\n" as String).data(using: String.Encoding.utf8)

    // Write it to the file
    theFile?.write(data!)

    // Close the file
    theFile?.closeFile()
}
else {
    print("Writing failed.")
}

} // End file writing

However, the FileHandler always returns nil. I suspect that this is because I'm supplying the path and/or filename incorrectly?

但是,FileHandler始终返回nil。我怀疑这是因为我提供的路径和/或文件名不正确?

I've been googling and stack overflowing for a couple of hours now... And still no idea of how to fix the problem. How do I specify the correct location and file?

我一直在谷歌搜索和堆栈溢出几个小时......但仍然不知道如何解决问题。如何指定正确的位置和文件?

2 个解决方案

#1


1  

You cannot write file outside of the application sandbox. Each iOS app have individual directories Document, Library, tmp to store data.

您无法在应用程序沙箱之外编写文件。每个iOS应用程序都有单独的目录Document,Library,tmp来存储数据。

func writeFile(){

    let documentsPath = NSSearchPathForDirectoriesInDomains(.documentationDirectory,.userDomainMask,true)
    let theFile: FileHandle? = FileHandle(forWritingAtPath: "\(documentsPath)/MyFiles/file.txt")

    if theFile != nil { // This is always nil!!!
        let data = ("Some text\nline 2\nline3\n" as String).data(using: String.Encoding.utf8)

        // Write it to the file
        theFile?.write(data!)

        // Close the file
        theFile?.closeFile()
    }
    else {
        print("Writing failed.")
    }

} // End file writing

#2


0  

I'm trying to overwrite whatever is in the file

我正在尝试覆盖文件中的任何内容

You can't. Writing into the app bundle is forbidden.

你不能。禁止写入应用程序包。

The proper approach in this kind of situation is, on first launch, to copy the files into, say, the Documents folder, which is writable. Now you can access them from there, and they are both readable and writable.

在这种情况下,正确的方法是,在首次启动时,将文件复制到例如可写的Documents文件夹中。现在你可以从那里访问它们,它们都是可读写的。

#1


1  

You cannot write file outside of the application sandbox. Each iOS app have individual directories Document, Library, tmp to store data.

您无法在应用程序沙箱之外编写文件。每个iOS应用程序都有单独的目录Document,Library,tmp来存储数据。

func writeFile(){

    let documentsPath = NSSearchPathForDirectoriesInDomains(.documentationDirectory,.userDomainMask,true)
    let theFile: FileHandle? = FileHandle(forWritingAtPath: "\(documentsPath)/MyFiles/file.txt")

    if theFile != nil { // This is always nil!!!
        let data = ("Some text\nline 2\nline3\n" as String).data(using: String.Encoding.utf8)

        // Write it to the file
        theFile?.write(data!)

        // Close the file
        theFile?.closeFile()
    }
    else {
        print("Writing failed.")
    }

} // End file writing

#2


0  

I'm trying to overwrite whatever is in the file

我正在尝试覆盖文件中的任何内容

You can't. Writing into the app bundle is forbidden.

你不能。禁止写入应用程序包。

The proper approach in this kind of situation is, on first launch, to copy the files into, say, the Documents folder, which is writable. Now you can access them from there, and they are both readable and writable.

在这种情况下,正确的方法是,在首次启动时,将文件复制到例如可写的Documents文件夹中。现在你可以从那里访问它们,它们都是可读写的。