2012-04-02 7 views
-2

私がアプリケーションを持っているし、アプリケーションデリゲートに私はドキュメントフォルダにDBファイルをコピーするためのコードを持っているが、前に働いて、そして与えていた理由ので、私は知らない働いていません次のエラーが表示されたら、何かを見つけることができたら幸いです。それは、 "ファイルが存在する" と言う中***アサーション失敗 - [AppDelegate createDatabaseExecutableFile]

2012-04-02 13:52:01.162 TurfNutritionTool_ver_5.1[2379:b903] *** Assertion failure in -[AppDelegate createDatabaseExecutableFile], /Development/TurfNutritionTool_IOS_5.1/TurfNutritionTool/AppDelegate.m:188 
    2012-04-02 13:52:01.165 TurfNutritionTool_ver_5.1[2379:b903] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Failed to create writable database file with message 'The operation couldn’t be completed. File exists'.' 
    *** Call stack at first throw: 
    (
     0 CoreFoundation      0x0179a5a9 __exceptionPreprocess + 185 
     1 libobjc.A.dylib      0x018ee313 objc_exception_throw + 44 
     2 CoreFoundation      0x01752ef8 +[NSException raise:format:arguments:] + 136 
     3 Foundation       0x011fc3bb -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 116 
     4 TurfNutritionTool_ver_5.1   0x00003fef -[AppDelegate createDatabaseExecutableFile] + 831 
     5 TurfNutritionTool_ver_5.1   0x00003486 -[AppDelegate application:didFinishLaunchingWithOptions:] + 86 
     6 UIKit        0x009f9c89 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1163 
     7 UIKit        0x009fbd88 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 439 
     8 UIKit        0x00a06617 -[UIApplication handleEvent:withNewEvent:] + 1533 
     9 UIKit        0x009feabf -[UIApplication sendEvent:] + 71 
     10 UIKit        0x00a03f2e _UIApplicationHandleEvent + 7576 
     11 GraphicsServices     0x01e21992 PurpleEventCallback + 1550 
     12 CoreFoundation      0x0177b944 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52 
     13 CoreFoundation      0x016dbcf7 __CFRunLoopDoSource1 + 215 
     14 CoreFoundation      0x016d8f83 __CFRunLoopRun + 979 
     15 CoreFoundation      0x016d8840 CFRunLoopRunSpecific + 208 
     16 CoreFoundation      0x016d8761 CFRunLoopRunInMode + 97 
     17 UIKit        0x009fb7d2 -[UIApplication _run] + 623 
     18 UIKit        0x00a07c93 UIApplicationMain + 1160 
     19 TurfNutritionTool_ver_5.1   0x00002ddd main + 125 
     20 TurfNutritionTool_ver_5.1   0x00002d55 start + 53 
     21 ???         0x00000001 0x0 + 1 
    ) 
    terminate called throwing an exception(lldb) 

答えて

2

:ありがとう

// Creates a writable copy of the bundled default database in the application Documents directory. 
- (void) createDatabaseExecutableFile { 

    // First, test for existence. 
    BOOL _successDB; 
    BOOL _successConfig; 
    NSFileManager* _fileManager = [NSFileManager defaultManager]; 
    NSError* _error; 
    NSArray* _paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString* _documentsDirectory = [_paths objectAtIndex:0]; 
    NSString* _writableDBPath = [_documentsDirectory stringByAppendingPathComponent:@"turfnutritiontool_ver.db"]; 
    NSString* _writableConfigPath = [_documentsDirectory stringByAppendingPathComponent:@"Configuration.plist"]; 

    _successDB = [_fileManager fileExistsAtPath:_writableDBPath]; 
    _successConfig = [_fileManager fileExistsAtPath:_writableConfigPath]; 

    if (_successDB && _successConfig) { 

     return; 
    } 

    // The writable database does not exist, so copy the default to the appropriate location. 
    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"turfnutritiontool_ver.db"]; 
    NSString *defaultConfigPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Configuration.plist"]; 

    _successDB = [_fileManager copyItemAtPath:defaultDBPath toPath:_writableDBPath error:&_error]; 
    _successConfig = [_fileManager copyItemAtPath:defaultConfigPath toPath:_writableConfigPath error:&_error]; 
    if (!_successDB || !_successConfig) { 

     NSAssert1(0, @"Failed to create writable database file with message '%@'.", [_error localizedDescription]); 
    } 
} 

これは誤りです。それは単なる手掛かりではない? NSFileManagerドキュメントから

If a file with the same name already exists at dstPath, this method aborts the copy attempt and returns an appropriate error.

ところで、あなたはエラーが最初のコピーで発生した場合ので、終わりではない、一度、各コピー後にエラーをチェックし、第二のコピーする必要があり、あなた最初のコピーからエラー情報を失います。

+0

OK友達、ありがとう、私はこれをチェックします – Retro

関連する問題