我正在使用以下代碼來複制sqlite數據庫,並且它工作正常。但是,當我將此代碼轉換爲swift時,它會在Bool類型上顯示錯誤。在Swift中複製Sqlite數據庫無法正常工作
這是客觀的C代碼
- (void) copyDatabaseIfNeeded {
//Using NSFileManager we can perform many file system operations.
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSString *dbPath = [self getDBPath];
BOOL success = [fileManager fileExistsAtPath:dbPath];
if(!success) {
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"database.sqlite"];
success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];
if (!success)
NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
}
}
- (NSString *) getDBPath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
return [documentsDir stringByAppendingPathComponent:@"database.sqlite"];
}
這是這是造成問題的雨燕CopyDataBase。
var fileManager = FileManager.default
var error: Error!
var dbPath = self.getDBPath()
var success = fileManager.fileExists(atPath: dbPath)
if !success {
var defaultDBPath = URL(fileURLWithPath: Bundle.main.resourcePath!).appendingPathComponent("CapalinoDataBase.sqlite").absoluteString
do {
success = try fileManager.copyItem(atPath: defaultDBPath, toPath: dbPath)
}
catch {
}
if !success {
assert(false, "Failed to create writable database file with message '\(error.localizedDescription)'.")
}
}