更新时间:2022-11-28 18:50:13
因此,您要删除所有 *。sqlite
文件?没有办法避免循环,但您可以通过使用 NSPredicate
首先过滤掉非sql文件并使用快速枚举确保快速性能来限制它。这里有一个方法:
So, you'd like to delete all *.sqlite
files? There is no way to avoid looping, but you can limit it by using a NSPredicate
to filter out non-sql files first and ensure speedy performance using fast enumeration. Here's a method to do it:
- (void)removeAllSQLiteFiles
{
NSFileManager *manager = [NSFileManager defaultManager];
// the preferred way to get the apps documents directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
// grab all the files in the documents dir
NSArray *allFiles = [manager contentsOfDirectoryAtPath:documentsDirectory error:nil];
// filter the array for only sqlite files
NSPredicate *fltr = [NSPredicate predicateWithFormat:@"self ENDSWITH '.sqlite'"];
NSArray *sqliteFiles = [allFiles filteredArrayUsingPredicate:fltr];
// use fast enumeration to iterate the array and delete the files
for (NSString *sqliteFile in sqliteFiles)
{
NSError *error = nil;
[manager removeItemAtPath:[documentsDirectory stringByAppendingPathComponent:sqliteFile] error:&error];
NSAssert(!error, @"Assertion: SQLite file deletion shall never throw an error.");
}
}