简体   繁体   English

将writeToFile:原子地:覆盖数据?

[英]Will writeToFile:atomically: overwrite data?

Really simple question but it doesn't seem to be clear in Apple's docs. 非常简单的问题,但在Apple的文档中似乎并不清楚。

Will writeToFile:atomically: for NSData , NSArray etc. overwrite existing data on a file? writeToFile:atomically:对于NSDataNSArray等覆盖文件上的现有数据?

Yes. 是。 It will. 它会。

Here are some characters to pass the 30 character limit. 以下是一些传递30个字符限制的字符。

Method writeToFile:atomically: ALWAYS OVERWRITE FILE , regardless of the atomically: YES or NO. 方法writeToFile:atomically: 总是覆盖文件 ,无论atomically:如何atomically:是或否。

Information from Apple docs (NSData, section "Saving Data"): 来自Apple文档的信息(NSData,“保存数据”部分):

The NSData class and its subclasses provide methods to quickly and easily save their contents to disk. NSData类及其子类提供了快速轻松地将其内容保存到磁盘的方法。 To minimize the risk of data loss, these methods provide the option of saving the data atomically. 为了最大限度地降低数据丢失的风险,这些方法提供了以原子方式保存数据的选项。 Atomic writes guarantee that the data is either saved in its entirety, or it fails completely. 原子写入保证数据保存完整,或者完全失败。 The atomic write begins by writing the data to a temporary file. 原子写入从将数据写入临时文件开始。 If this write succeeds, then the method moves the temporary file to its final location. 如果此写入成功,则该方法将临时文件移动到其最终位置。

No one word about checking if file exist. 没有人关于检查文件是否存在。

For example, for method copyItemAtPath:toPath:error: in docs (NSFileManager, section Discussion) specifically written about such checking: 例如,对于方法copyItemAtPath:toPath:error:docs (NSFileManager,section Discussion)中专门写了关于这样的检查:

If a file with the same name already exists at dstPath, this method stops the copy attempt and returns an appropriate error. 如果dstPath上已存在具有相同名称的文件,则此方法将停止复制尝试并返回相应的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM