iPhone:找不到资源

时间:2009-08-02 20:18:38

标签: iphone objective-c cocoa-touch

在我的应用程序中,我想将文件从我的包中复制到文档目录,以便对其进行修改。

这是我的代码:

+(BOOL) copyDB: (NSString*) pdbName {
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    NSString *dbPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES) objectAtIndex: 0];
    dbPath = [dbPath stringByAppendingPathComponent: @"myApp"];
    dbPath = [dbPath stringByAppendingPathComponent: @"Profiles"];
    dbPath = [dbPath stringByAppendingPathComponent: @"ES-EN"];

    dbPath = [dbPath stringByAppendingPathExtension: @"prof"];

    if([fileManager fileExistsAtPath: dbPath]) {
        DebugLog(@"file exists at path %@", dbPath);
        return FALSE;
    }

    NSString *defaultDBPath = [ [ [NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: @"ES-EN"];
    defaultDBPath = [defaultDBPath stringByAppendingPathExtension: @"prof"];

    DebugLog(@"dbPath: %@", dbPath);
    DebugLog(@"defaultDBPath: %@", defaultDBPath);

    if(![fileManager fileExistsAtPath: defaultDBPath]) {
        DebugLog(@"Cannot find resource file");
        return FALSE;
    }

    BOOL success = [fileManager copyItemAtPath: defaultDBPath toPath: dbPath error: &error];

    if (!success) {
        DebugLog(@"!success: Failed to create writable database file with message '%@'.", [error localizedDescription]);
        UIAlertView *msg = [[UIAlertView alloc]
                            initWithTitle:@"Error"
                            message: [NSString stringWithFormat: @"Failed to create writable database file with message '%@'.", [error localizedDescription] ]
                            delegate:self cancelButtonTitle:@"OK"
                            otherButtonTitles:nil, nil];
        [msg show];
        [msg release];
        return FALSE;
    }

    [ [UserProfile sharedUserProfile] writeInitialData: pdbName];

    //Creating fruits folder
    NSString *dicDir = [Fruits_Dir stringByAppendingPathComponent: pdbName];

    //Does a directory exist?
    if ( ![fileManager fileExistsAtPath: dicDir] )
    {
        //Create a directory
        DebugLog(@"Creating userName dir");
        if (![fileManager createDirectoryAtPath: dicDir attributes:nil])
        {
            DebugLog(@"failed to create dicDir");
        }
    }
    return TRUE;
}

我认为这段代码应该可行。它以某种方式在模拟器上工作,但它不能在设备上工作。我得到一个无法找到资源文件的日志。

可能是什么问题?

感谢。

1 个答案:

答案 0 :(得分:1)

这种情况的一种方式是资源文件被意外删除,作为XCode项目的参考。

如果文件曾经存在,则可能已将其安装到模拟器应用程序目录中。即使从XCode中删除它,文件也会保留在那里,你的应用程序将在模拟器中运行。

但是只要你在设备上安装它,文件就会丢失。

确认该文件已列在您的XCode项目资源中,并从模​​拟器中完全删除该应用程序,然后再次尝试将所有内容同步。