2009-08-02 34 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

1つの違いがあります。コンピュータは大文字と小文字を区別しないことが多く、iPhoneでは大文字と小文字が区別されます。 I.あなたのリソースファイルは実際に "ES-EN.Prof"という名前になっていますか? – epatel

+0

正確に "ES-EN.prof"という名前です... –

+0

pathForResource:ofType:yieldとは何ですか? – mjhoy

答えて

1

これは、リソースファイルが誤ってXCodeプロジェクトからの参照として削除されたことが起こる1つの方法です。

ファイルがある場合は、シミュレータのアプリケーションディレクトリにインストールされている可能性があります。 XCodeからファイルを削除しても、ファイルはそこにとどまり、あなたのアプリはシミュレータで動作します。

ただし、デバイスにクリーンインストールするとファイルが失われます。

ファイルがXCodeプロジェクトのリソースにリストされていることを確認し、シミュレータからアプリケーションを完全に削除してからもう一度試してみてください。

+0

"デバッグ"ではなく "リリース"の設定でアプリを再構築しましたが、今回はすべてのリソースがコピーされるように見えます。 どうなりますか? :) –

+0

アプリケーションをビルドするとき、すべてのリソースが(適切に)「リソースのコピー」と呼ばれる段階でコピーされます。壊れた構成の構築フェーズを確認し、リソースがフェーズの一部であることを確認します。 – Tim

関連する問題