I am trying to remove the contents text file for iOS.
Alternatively, I could delete the file then re-create it right after with no contents.
For this solution, I know I can delete the file using [fileManager removeItemAtPath:filePath error:&error];
. Recreating the file is the hard part for me.
I am locating the file just fine using the following code:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"myFile.txt"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if([fileManager fileExistsAtPath:filePath])
{
[fileManager removeItemAtPath:filePath error:&error];
}
//code to recreate the same file, just with no content
Try
-[[NSFileManager defaultManager] createFileAtPath:path contents:[NSData data] attributes:nil]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With