2009-08-02 114 views
0

在我的应用程序中,我想将我的包中的文件复制到文档目录以修改它。iPhone:找不到资源

这里是我的代码:

+(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; 
} 

我想,这个代码应工作。不知何故,它在模拟器上应该如此,但它不适用于设备。我得到一个资源文件找不到的日志。

可能是什么问题?

谢谢。

+0

一个区别可以是情况。计算机通常不区分大小写,并且iPhone区分大小写。 I.E.你的资源文件实际上被命名为“ES-EN.Prof”? – epatel 2009-08-02 20:26:13

+0

它被完全命名为“ES-EN.prof”... – 2009-08-02 20:39:24

回答

1

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

如果该文件曾经存在,那么它可能已被安装到模拟器应用程序目录中。即使将它从XCode中移除后,该文件仍会保留在那里,您的应用程序将在模拟器中运行。

但是,只要您在设备上安装干净,文件将会丢失。

确认该文件已列在您的XCode项目资源中,并将该应用程序从模拟器中彻底删除,然后再次尝试使其同步。