【问题标题】:writeToFile how to tell when it is completedwriteToFile 如何判断何时完成
【发布时间】:2012-04-06 21:05:00
【问题描述】:

我正在向我的文档目录写入一些数据,我想知道是否有办法告诉我的 writeToFile 方法何时完成以及我正在创建的文件已完全创建。在此先感谢这是我正在调用的方法我只是在寻找一种方法,它是委托方法还是其他方式来判断何时完成。

[imageData writeToFile:fullPathToFile atomically:YES];

尼克

【问题讨论】:

    标签: iphone ios


    【解决方案1】:

    writeToFile:atomically 方法是“同步的”。它将写入文件,然后根据文件是否写入成功返回YESNO

    这意味着只要方法返回,操作就完成了。

    BOOL success = [imageData writeToFile:fullPathToFile atomically:YES];
    // Now, the operation is complete
    // success indicates whether the file was successfully written or not
    

    【讨论】:

    • 如果保存不成功,有没有办法获取错误日志?
    • @Patrick 你可以使用writeToFile:options:error:的方法
    • 请使用 Swift 版本!
    【解决方案2】:

    斯威夫特 5:

    do {
        try data.write(to: path)
        // here's when write operation is completed without errors thrown
    } catch {
        Swift.print(error)
    }
    

    【讨论】:

      猜你喜欢
      • 2021-12-04
      • 1970-01-01
      • 2013-04-10
      • 1970-01-01
      • 1970-01-01
      • 2018-03-01
      • 2012-12-04
      • 2013-01-10
      • 1970-01-01
      相关资源
      最近更新 更多