真的很简单的问题,但它似乎没有在苹果的文档中很清楚。将writeToFile:原子地覆盖数据?
请问writeToFile:atomically:
是否为NSData
, NSArray
等覆盖文件上的现有数据?
真的很简单的问题,但它似乎没有在苹果的文档中很清楚。将writeToFile:原子地覆盖数据?
请问writeToFile:atomically:
是否为NSData
, NSArray
等覆盖文件上的现有数据?
是的。它会。
以下是一些要传递30个字符限制的字符。
方法writeToFile:atomically:
总是覆盖文件,不管atomically:
是或否。
从Apple docs(NSData的,部分 “保存数据”)的信息:
The NSData class and its subclasses provide methods to quickly and easily save their contents to disk. 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.
没有一个字有关,如果文件存在检查。
例如,对于方法copyItemAtPath:toPath:error:
在docs(的NSFileManager,部分讨论)专门写了这样的检查:
If a file with the same name already exists at dstPath, this method stops the copy attempt and returns an appropriate error.
+1“记录”你多余的字符:) – Julian
无论你做原子与否没有按没关系;在任何一种情况下,文件都将被新数据完全覆盖。 –
啊,但对我来说,它失败了,原始文件完好无损。由于writeToFile不会返回NSError,因此如何调查失败?这可能是一个权限问题,但我不能确定。我的代码作为安装程序插件包运行,需要root权限的安装包 - 我不知道如何验证/打印我的代码运行的权限。 –