Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/96.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用FileHandler写入文件时出现问题_Ios_Swift3_Filehandle - Fatal编程技术网

Ios 使用FileHandler写入文件时出现问题

Ios 使用FileHandler写入文件时出现问题,ios,swift3,filehandle,Ios,Swift3,Filehandle,我有一个如下代码: let fileName = "name.txt" let fileURL = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(fileName) try! "".write(to: fileURL, atomically: true, encoding: String.Encoding.utf8) let fileHandle = try! FileHandle(forWritingTo

我有一个如下代码:

let fileName = "name.txt"
let fileURL = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(fileName)
try! "".write(to: fileURL, atomically: true, encoding: String.Encoding.utf8)
let fileHandle = try! FileHandle(forWritingTo: fileURL)
fileHandle.seekToEndOfFile()
这个代码是有效的。但如果我去掉这条线:

try! "".write(to: fileURL, atomically: true, encoding: String.Encoding.utf8)
我得到运行时异常。我不知道为什么需要这条线?

请按此使用

let fileName = "name.txt"
let fileURL = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(fileName)

let text = ""

 do
 {
    try text.write(to: fileURL, atomically: false, encoding: .utf8)
 }
 catch {
        print("error" , error)
 }
这样用

let fileName = "name.txt"
let fileURL = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(fileName)

let text = ""

 do
 {
    try text.write(to: fileURL, atomically: false, encoding: .utf8)
 }
 catch {
        print("error" , error)
 }

当我像这样运行带有错误消息print的代码时

    do {
        let fileHandle = try FileHandle(forWritingTo: fileURL)
        fileHandle.seekToEndOfFile()
    } catch {
        let nsError = error as NSError
        print("ERROR : " + nsError.localizedDescription)
    }
我收到一条错误消息说
“错误:操作无法完成。(错误2)。”

这和这个问题是一样的。

您需要在调用之前创建文件

FileHandle(forWritingTo: fileURL)
所以,这应该行得通

    let fileName = "test.txt"
    let textContent = "foobar"

    if let fileURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).last {
       let filePath = fileURL.appendingPathComponent(fileName)

       do {
          try textContent.write(to: filePath, atomically: true, encoding: String.Encoding.utf8)
       } catch let error as NSError {
          print("error: \(error)")
       }
    }

别忘了检查错误消息:)

当我像这样运行带有错误消息打印的代码时

    do {
        let fileHandle = try FileHandle(forWritingTo: fileURL)
        fileHandle.seekToEndOfFile()
    } catch {
        let nsError = error as NSError
        print("ERROR : " + nsError.localizedDescription)
    }
我收到一条错误消息说
“错误:操作无法完成。(错误2)。”

这和这个问题是一样的。

您需要在调用之前创建文件

FileHandle(forWritingTo: fileURL)
所以,这应该行得通

    let fileName = "test.txt"
    let textContent = "foobar"

    if let fileURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).last {
       let filePath = fileURL.appendingPathComponent(fileName)

       do {
          try textContent.write(to: filePath, atomically: true, encoding: String.Encoding.utf8)
       } catch let error as NSError {
          print("error: \(error)")
       }
    }

不要忘记检查错误消息:)

如果查看文档中的,返回值指定为:

初始化的文件句柄对象,如果url上不存在文件,则为nil

文件必须存在,否则返回
nil

创建文件

如果您未创建文件且文件不存在,尝试使用
FileHandle
打开文件将使应用程序崩溃

如果显式创建文件,代码可能更可读:

FileManager.default.createFile(atPath: fileURL.path, contents: nil)

如果查看文档中的,返回值指定为:

初始化的文件句柄对象,如果url上不存在文件,则为nil

文件必须存在,否则返回
nil

创建文件

如果您未创建文件且文件不存在,尝试使用
FileHandle
打开文件将使应用程序崩溃

如果显式创建文件,代码可能更可读:

FileManager.default.createFile(atPath: fileURL.path, contents: nil)

更好的实现是使用POSIX文件描述符,它允许您在一次操作中创建不存在的文件或将其截断为零长度(还允许更多标志,例如用于独占锁定):


更好的实现是使用POSIX文件描述符,它允许您在一次操作中创建不存在的文件或将其截断为零长度(还允许更多标志,例如用于独占锁定):