2

I want to save each NSData object to a temp directory in iOS and I want each new object to overwrite the old one.

Objects will be of different formats and of different sizes.

I know how do you get the path to the NSTemporaryDirectory but how on Earth can you overwrite old NSData file in there each time a new one is added?

Sergey Grischyov
  • 11,995
  • 20
  • 81
  • 120

1 Answers1

8

Get the file.

Remove it from NSTemporaryDirectory.

[[NSFileManager defaultManager] removeItemAtPath:filePath error:&error];

Create a new file with same name and save it there.

[[NSData data] writeToFile:filePath options:NSDataWritingAtomic error:nil];

Or Simply you can over-write the file.

NSData *data = <all data>;
[data writeToFile:targetPath atomically:YES];
Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140