1
我有一段代码添加一个数据库,如果它还没有被创建。 下面是代码数据库未被创建
-(void) checkAndCreateDatabase{
// Check if the SQL database has already been saved to the users phone, if not then copy it over
NSString *path=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *database_path=[path stringByAppendingPathComponent:@"Favorite_Database.sqlite"];
// Create a FileManager object, we will use this to check the status
// of the database and to copy it over if required
// Check if the database has already been created in the users filesystem
BOOL success = [[NSFileManager defaultManager] fileExistsAtPath:database_path];
// If the database already exists then return without doing anything
if(success) {
//[fileManager removeFileAtPath:databasePath handler:nil];
return;
}
// If not then proceed to copy the database from the application to the users filesystem
// Get the path to the database in the application package
NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Favorite_Database.sqlite"];
// Copy the database from the package to the users filesystem
[[NSFileManager defaultManager] copyItemAtPath:databasePathFromApp toPath:database_path error:nil];
}
当它击中线BOOL success = [[NSFileManager defaultManager] fileExistsAtPath:database_path];
它只是跳过,其余并继续上。我没有得到if语句。我需要它来检查,所以我可以添加这个数据库
我怎样才能让它正常工作?
在我的代码和代码都停靠在'如果(fileExistsAtPath)'然后就跳过其余没有任何错误 – BigT
嗯 - 你可以尝试加入一些记录和运行在模拟器和设备上? (试图排除文件系统/权限等...在虚拟器中) – bryanmac
也 - 当您在模拟器中运行时,请确保您可以注销并访问该终端中的路径。 – bryanmac