How to empty a folder in Objective-C ?
A must have method! I always use it to erase my log files.
/** Empty a folder contents.
@param folder Full path of the folder to be emptied.
@return Returns true on success, false otherwise.
*/
+ (BOOL)emptyFolder:(NSString *)folder {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *err;
NSArray *tmpFiles = [fileManager contentsOfDirectoryAtPath:folder error:&err];
if (err) {
LogError(@"Error occured: %@", err);
return NO;
}
for (NSString *file in tmpFiles) {
[fileManager removeItemAtPath:[folder stringByAppendingPathComponent:file] error:&err];
if (err) {
LogError(@"Error occured: %@", err);
return NO;
}
}
return YES;
}
Download the ready-for-use source file (.m) of Empty a directory in your iOS App
No comments:
Post a Comment