Satheeshkumar Naidu Satheeshkumar Naidu - 3 months ago 13
Objective-C Question

how to delete the specific item in the document directory ios objective c

I want remove specific item from

DocumentDirectory
i.e I have 50 objects in the
DocumentDirectory
and i want to remove the 5 different elements from it. I used following code to get the
DocumentDirectory
objects.

NSURL *vedioURL;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDirectory = [paths objectAtIndex:0];

NSArray *filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory error:nil];

NSLog(@"files array %@", filePathsArray);


can any one Help me out to solve this problem

Answer

You can try like this way

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
if (filePathsArray.count >= 6) {
    for (NSInteger i=filePathsArray.count-6; i < filePathsArray.count; i++) {
        NSString *filePath = [documentsDirectory stringByAppendingPathComponent:[filePathsArray objectAtIndex:i]];
        if ([[NSFileManager defaultManager] fileExistsAtPath:filePath])  {
           NSError *error;
            if (![[NSFileManager defaultManager] removeItemAtPath:filePath error:&error]) {
                NSLog(@"Delete error: %@", error);
            }
        }
    }
}
Comments