Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/39.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
Iphone 从主捆绑包复制会创建大小为零kb的文件_Iphone_Nsbundle - Fatal编程技术网

Iphone 从主捆绑包复制会创建大小为零kb的文件

Iphone 从主捆绑包复制会创建大小为零kb的文件,iphone,nsbundle,Iphone,Nsbundle,作为我应用程序启动的一部分,我将捆绑包文件复制到我的文档目录 这适用于我的四个文件中的三个,但第四个文件创建的是零KB文件 在iOS 5.0 sim卡上运行。我已经清理了几次构建,并检查了文件名是否正确 该文件显示在目录中,但大小为零kb,应为24K 谢谢你的帮助 -(BOOL) CheckDBs: (NSString *)dbname { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory

作为我应用程序启动的一部分,我将捆绑包文件复制到我的文档目录

这适用于我的四个文件中的三个,但第四个文件创建的是零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中已存在同名文件,则此方法 中止复制尝试并返回相应的错误

请确保目标为空,然后重试。另外,检查
错误
对象

如果所有检查结果都是错误的,那么文件名的拼写肯定是错误的。检查bundle、NSLog中是否存在确切的文件,无论您在哪里使用文件名或路径等。您应该会找到错误。同时检查Finder中的相应文件夹

而不是使用

[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:dbname]
试一试


好的,我找到了问题的原因

在我运行应用程序时,appdidfinishlaunching方法在加载一个视图控制器之前未完成。该视图控制器尝试访问从捆绑包复制的一个文件

我猜当您尝试访问数据库时,sqlite会创建文件,它创建的文件长度为零字节

因此,当我的appdidfinish启动方法检查由于sql调用而存在的文件是否存在时

这通常只是在第一次运行应用程序之前出现的问题,因为在第一次运行应用程序之后,数据库将存在


现在的问题是,如何让appdidfinish启动在允许启动其余部分之前完成,因为所讨论的视图控制器是主窗口的一部分。xib

包括所讨论的代码,以便我们可以看到您做错了什么。包括的代码日志给我一个1(即,文件存在于路径位置。我调用上述代码,如so dbname=@“UserProfile.db”;if(![self-CheckDBs:dbname]){//检查Documents目录[self-copyDBs:dbname]中是否存在User.db;//copy bundled db}是的,当我在finder中选择对捆绑包显示包内容时,它显示资源为24kb。然后,当我检查documents文件夹时,该文件显示为零kb。没有乐趣…sim重置没有任何区别我没有收到错误。我得到的是checkDBs方法返回1,即使该文件不存在。我共有4个文件pe这是文件号3,所有其他文件都可以复制。如果我手动从捆绑包复制到documents目录,它可以正常工作ok我想知道是不是因为文件名大于8个字符……我要测试一下,看看ok我在添加tabbarcontroller之前将firstrun方法移动到了,并且在启动vi之前完成了ewcontroller加载方法。。。
[[NSBundle mainBundle] pathForResource:shortName ofType:@"db"]