photosynthesis
photosynthesis

Reputation: 2890

How to delete files cached in supporting files?

I use the following code to store an NSMutableArray into a file:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePathHit = [documentsDirectory stringByAppendingPathComponent:@"hit"];        
[NSKeyedArchiver archiveRootObject:self.arrayHit toFile:filePathHit];

and the code below to retrieve data from the file:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePathHit = [documentsDirectory stringByAppendingPathComponent:@"hit"];

NSMutableArray *unarchivedHit = [NSKeyedUnarchiver unarchiveObjectWithFile:filePathHit];

It works fine but the file been written to seems reside there all the time, so each time I relaunch my application, it reads data from there. But what I want to achieve is the data only resides there during the application operates, and once the app's shut down the data been cleared. How can I achieve this? Thanks.

Upvotes: 0

Views: 180

Answers (1)

drewag
drewag

Reputation: 94723

You can use NSFileManager:

NSError *error = nil;
[[NSFileManager defaultManager] removeItemAtPath:filePathHit error:&error];

Upvotes: 2

Related Questions