【发布时间】:2013-09-03 06:55:52
【问题描述】:
我想检查文档文件夹中的一个文件。如果该文件在文件夹中不存在,我希望将其从主包复制到文档文件夹。我写了这段代码和这个文件要复制,但我的问题在这里.....!!!!我的文件是 .sqlite 文件(数据库文件),当复制到文档文件夹时,自己没有数据!!!为什么???而主包中的这个文件有自己的数据。
这是我的代码,但我不知道为什么不起作用!!!
#define DataName @"mydatabase.sqlite"
- (void)viewDidLoad
{
[self CopyDatabase];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (NSString*)DatabasePath
{
NSArray *Paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *DocumentDir = [Paths objectAtIndex:0];
//NSLog(@"%@",DocumentDir);
return [DocumentDir stringByAppendingPathComponent:DataName];
}
- (void)CopyDatabase
{
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
success = [fileManager fileExistsAtPath:[self DatabasePath]];
NSString *FileDB = [[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:DataName];
if (success)
{
NSLog(@"File Exist");
return;
}
else
{
[fileManager copyItemAtPath:FileDB toPath:[self DatabasePath] error:nil];
}
}
【问题讨论】:
-
显示
DataName的定义。同时使用NSLog()记录生成的FileDB字符串。也不要传error:nil;使用这种机制来获得更好的错误报告……这 2 分钟的努力是值得的。 -
1.您应该以小写字母开头的方法名称。 2.如果传递一个指向非对象的指针,不要使用
nil,而是使用NULL。 -
它可能正在复制您以前在捆绑包中的旧文件,或者它可能已将旧副本保留在您的文档文件夹中并且没有替换它。您可能需要在复制之前清除文档文件夹。
-
我好糊涂!!!!!!请帮帮我
-
[fileManager copyItemAtPath:FileDB toPath:[self DatabasePath] error:nil]; .. 使用错误来查看问题所在。
标签: ios objective-c sqlite