writeToFile如何判断它何时完成

Krz*_*ski 17 iphone ios

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

[imageData writeToFile:fullPathToFile atomically:YES];
Run Code Online (Sandbox Code Playgroud)

缺口

sch*_*sch 33

该方法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
Run Code Online (Sandbox Code Playgroud)

  • @Patrick您可以使用方法[writeToFile:options:error:](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSData_Class/Reference/Reference.html#// apple_ref/OCC/instm/NSData的/将writeToFile:选项:错误:) (3认同)

ate*_*kov 7

斯威夫特5:

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