这是我的代码:
- (void)copyFileIfNeeded
{
NSFileManager *fileManager= [NSFileManager defaultManager];
NSError *error;
NSString *filePath = [self getFile];
BOOL success = [fileManager fileExistsAtPath:filePath];
if ( !success )
{
NSString *defaultFilePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"BQWDatabase.db"];
success = [fileManager copyItemAtPath:defaultFilePath toPath:filePath error:&error];
}
}
- (NSString *)getFile
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
return [documentsDir stringByAppendingPathComponent:@"BQWDatabase.db"];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
[self copyFileIfNeeded];
return YES;
}
但是Documents文件夹总是空的,我该如何解决?谢谢。
编辑:
答案 0 :(得分:1)
您确定构建中包含BQWDatabase.db
吗?您可以通过打开XCode中的右侧窗格进行检查,选择顶部最左侧的图标,然后展开“目标成员资格”。
答案 1 :(得分:0)
我在自己的代码中做了类似的事情,除了我找到默认文件如下:
NSString *defaultFilePath = [[[NSBundle mainBundle] pathForResource:@"BQWDatabase" ofType:@"db"];
不确定它是否有所作为