Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will writeToFile:atomically: overwrite data?

Really simple question but it doesn't seem to be clear in Apple's docs.

Will writeToFile:atomically: for NSData, NSArray etc. overwrite existing data on a file?

like image 326
Jiho Kang Avatar asked Oct 20 '11 00:10

Jiho Kang


2 Answers

Yes. writeToFile:atomically: will.

like image 144
BJ Homer Avatar answered Nov 03 '22 20:11

BJ Homer


Method writeToFile:atomically: ALWAYS OVERWRITE FILE, regardless of the atomically: YES or NO.

Information from Apple docs (NSData, section "Saving Data"):

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.

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:

If a file with the same name already exists at dstPath, this method stops the copy attempt and returns an appropriate error.

like image 7
Igor Avatar answered Nov 03 '22 20:11

Igor