Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios NSFileManager removeItemAtPath不删除文件_Ios_Nsfilemanager - Fatal编程技术网

Ios NSFileManager removeItemAtPath不删除文件

Ios NSFileManager removeItemAtPath不删除文件,ios,nsfilemanager,Ios,Nsfilemanager,因此,我搜索了网络和stackoverflow,检查了上面提到的线程。这个文本框已经尽可能地问了问题,但是答案或提示对我没有帮助,因为代码一开始是好的,没有什么问题,应该没有问题,因为它没有那么难。但由于某些原因,我无法删除先前创建的文件。守则: NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true); NSString *dbFile = [[NSS

因此,我搜索了网络和stackoverflow,检查了上面提到的线程。这个文本框已经尽可能地问了问题,但是答案或提示对我没有帮助,因为代码一开始是好的,没有什么问题,应该没有问题,因为它没有那么难。但由于某些原因,我无法删除先前创建的文件。守则:

NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true);
NSString *dbFile = [[NSString alloc] initWithString:[dirPaths[0] stringByAppendingPathComponent:@"database.db"]];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
BOOL fileExists = [fileManager fileExistsAtPath:dbFile];
NSLog(@"Path to file: %@", dbFile);
NSLog(@"File exists: %d", fileExists);
NSLog(@"Is deletable file at path: %d", [fileManager isDeletableFileAtPath:dbFile]);
if (fileExists) {
    BOOL success = [fileManager removeItemAtPath:dbFile error:&error];
    if (!success) { NSLog(@"Error: %@", [error localizedDescription]); }
    else { NSLog(@"probably deleted the file!"); }
}
输出:

2014-09-15 17:29:25.058 test[13862:60b] Path to file: /var/mobile/Applications/BB708129-C504-4E84-9C08-DF4ACE1369DC/Documents/database.db
2014-09-15 17:29:25.060 test[13862:60b] File exists: 1
2014-09-15 17:29:25.061 test[13862:60b] Is deletable file at path: 1
2014-09-15 17:29:25.063 test[13862:60b] probably deleted the file!

但每次我运行代码时,它都会说文件存在,不会被删除。我遗漏了什么吗?

您是否有每次创建文件的代码?删除后,添加对
fileExistsAtPath:
的调用以确认它已消失。问题可能不在
NSFileManager
的范围内。正如@rmaddy所提到的,查看负责创建文件的代码,确保删除后不会重新创建文件(例如:打开和SQLite数据库连接将创建不存在的文件)。好的,你是对的。我正在使用sqlite3,如果它不存在,函数sqlite3_open()将创建一个db文件。我有一个db初始化器/创建者和一个select函数。我正在使用select函数检查其中的文件和行是否仍然存在。里面还有一个sqlite3_open()函数。。。奇怪的是,函数的file变量(dbpath=[dbFile UTF8String];)被注释了,所以我认为这会停止创建文件。非常感谢你们两位!