我正在做 Ray Wenderlich 的核心数据教程的第二部分。我没有读取包含默认数据的文件,而是尝试读取在他的 SQLite3 教程中创建的实际 Failed Bank 数据文件。当我尝试创建商店并打开文件时,程序抛出异常。
这是代码片段
- (NSPersistentStoreCoordinator *) persistentStoreCoordinator
{
if (_persistentStoreCoordinator != nil) {
return _persistentStoreCoordinator;
}
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"banklist.sqlite3"];
if (![[NSFileManager defaultManager] fileExistsAtPath:[storeURL path]]) {
NSURL *preloadURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"banklist" ofType:@"sqlite3"]];
NSError *err = nil;
if (![[NSFileManager defaultManager] copyItemAtURL:preloadURL toURL:storeURL error:&err]) {
NSLog(@"Opps, couldn't copy preloaded data");
}
}
NSError *error = nil;
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return _persistentStoreCoordinator;
}
这是一个例外:
2013-09-16 20:19:57.549 FailedBankCD[4086:c07] CoreData: 错误: (1) /Users/artletter1/Library/Application Support/iPhone Simulator/6.1/Applications/524B84E3-94D6 处的数据库 I/O 错误-40A3-8847-DD15A99B22A2/Documents/banklist.sqlite3。SQLite 错误代码:1,'没有这样的表:Z_METADATA' 2013-09-16 20:19:57.552 FailedBankCD[4086:c07] 未解决的错误 Error Domain=NSCocoaErrorDomain Code=256 “操作无法完成。(Cocoa 错误256.)" UserInfo=0x854b560 {NSUnderlyingException=/Users/artletter1/Library/Application Support/iPhone Simulator/6.1/Applications/524B84E3-94D6-40A3-8847-DD15A99B22A2/Documents/banklist.sqlite3 处数据库的 I/O 错误。SQLite 错误代码:1,'没有这样的表:Z_METADATA',NSSQLiteErrorDomain=1},{ NSSQLiteErrorDomain = 1; NSUnderlyingException = " /Users/artletter1/Library/Application Support/iPhone Simulator/6.1/Applications/524B84E3-94D6-40A3-8847-DD15A99B22A2/Documents/banklist.sqlite3 处的数据库 I/O 错误。SQLite 错误代码:1,'没有这样的表:Z_METADATA'"; }
谢谢!
罗伊