簡體   English   中英

iOS錯誤:由於未捕獲的異常'NSInternalInconsistencyException'而終止應用程序

[英]iOS error: Terminating app due to uncaught exception 'NSInternalInconsistencyException'

我是一名新開發人員,但遇到此錯誤。 我累了要修復它,方法是兩次添加libsqlite3.0.dylib並重新啟動x代碼。 甚至我添加了libsqlite3.dylib的另一個庫,但didint起作用了。 錯誤:由於未捕獲的異常“ NSInternalInconsistencyException”而終止應用程序,原因:“無法創建帶有消息的可寫數據庫文件

-(void)copyDatabaseIfNeeded
{
    NSFileManager * filemanger = [NSFileManager defaultManager];
    NSError *error;
    NSString *dbpath= [self getDbPath];
    BOOL success = [filemanger fileExistsAtPath:dbpath];

    if(!success)
    {
        NSString *defaultDBpath= [[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:@"myDatabase.sqlite"];
        success = [filemanger copyItemAtPath:defaultDBpath toPath:dbpath error:&error];
        if(!success)
        {
            NSAssert1(0, @"Failed to create writeable database file with message '@' .", [error localizedDescription]);
        }
    }

}
-(NSString *)getDbPath
{
    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [path objectAtIndex:0];
    return[documentsDir stringByAppendingPathComponent:@"myDatabase.sqlite"];
}

在這里,此功能將打開一個數據庫,如果文檔中不存在該數據庫,則會從包中復制它。 您是否需要從頭開始創建一個?

-(BOOL) openDB: (NSString *) dbName{
    NSString *dbPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    dbPath = [dbPath stringByAppendingPathComponent:dbName];

    NSFileManager *fileManager = [NSFileManager defaultManager];

    if (![fileManager fileExistsAtPath:dbPath]) {

        NSString *sourcePath = [[[NSBundle mainBundle] resourcePath ]stringByAppendingPathComponent:dbName];   
        [fileManager copyItemAtPath:sourcePath toPath:dbPath error:nil];
    }

    if(!(sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK))           
    {               
       NSLog(@"An error has occured opening Sqlite");  
       return FALSE;
    }

    return TRUE;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM