代码之家  ›  专栏  ›  技术社区  ›  Ilya Suzdalnitski

iPhone:找不到资源

  •  0
  • Ilya Suzdalnitski  · 技术社区  · 15 年前

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

    以下是我的代码:

    +(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 回复  |  直到 15 年前
        1
  •  1
  •   elasticrat    15 年前

    发生这种情况的一种方法是将资源文件作为引用从Xcode项目中意外删除。

    如果该文件曾经存在过,那么它很可能已经安装到模拟器应用程序目录中。即使将其从Xcode中删除,该文件仍将保留在那里,您的应用程序将在模拟器中工作。

    但是一旦你把它安装到设备上,文件就会丢失。

    确认Xcode项目资源中列出了该文件,并从模拟器中完全删除该应用程序,然后再次尝试,以使所有内容同步。