![](/img/trans.png)
[英]iPhone (iOS): copying files from main bundle to documents folder error
[英]copying from main bundle creates a file size zero kb
作为我的应用程序启动的一部分,我将捆绑文件复制到我的文档目录中。
我的四个文件中的三个都可以正常工作,但是第四个文件可以创建一个零KB文件。
在iOS 5.0 sim上运行。 我已经多次清理了构建,并检查了文件名大小写是否正确。
该文件出现在目录中,但为零kb,应为24K
任何帮助表示赞赏。
-(BOOL) CheckDBs: (NSString *)dbname
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *dbPath = [documentsDir stringByAppendingPathComponent:dbname];
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL success = [fileManager fileExistsAtPath: dbPath];
NSLog(@"AppDelegate CheckDatabase: %@ = %i", dbPath, success);
if (success) {
//NSLog(@"return YES");
return YES;
}
else {
return NO;
}
} // Complete - checks if files exist in the User Documents directory
-(void) copyDBs: (NSString *) dbname
{
//Using NSFileManager we can perform many file system operations.
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *dbPath = [documentsDir stringByAppendingPathComponent:dbname];
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:dbname];
BOOL success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];
if (success) {
// Version 4.0 code
//NSDictionary *attribs = [NSDictionary dictionaryWithObject:NSFileProtectionComplete forKey:NSFileProtectionKey];
//success = [fileManager setAttributes:attribs ofItemAtPath:dbPath error:&error];
NSLog(@"AppDelegate copyDatase: %@ = %d", dbPath, success);
}
//NSLog(@"AppDelegate copyDatase: %@ = %d", dbPath, success);
if (!success) {
NSLog(@"Failed to copy database: '%@'", [error localizedDescription]);
// NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
}
}
您还检查了原始文件大小吗?
尝试重置您的模拟器。 从NSFileManager
文档中:
如果dstPath中已经存在同名文件,则此方法将中止复制尝试,并返回适当的错误。
确保目的地为空,然后重试。 另外,检查error
对象。
如果全部检查出,文件名拼写肯定出错。 检查确切的文件是否存在于捆绑软件,NSLog中,无论您在何处使用文件名或路径等。您都应该找到错误。 还要在Finder中检查相应的文件夹。
而不是使用
[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:dbname]
尝试
[[NSBundle mainBundle] pathForResource:shortName ofType:@"db"]
好的,我弄清楚是什么原因引起的。
当我运行应用程序时,在加载视图控制器之一之前,appdidfinishlaunching方法未完成。 该视图控制器尝试访问从分发包复制过来的文件之一。
我猜sqlite在尝试访问数据库时会创建文件,它会以零字节长度创建文件。
因此,当我的appdidfinish启动方法检查文件是否存在时,由于sql调用而导致文件存在。
这通常只会在应用程序首次运行之前出现问题,因为之后数据库将存在。
现在的问题是,在有问题的视图控制器是mainwindow.xib的一部分时,如何使appdidfinish启动完成,然后其余的启动?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.