私はios 5.1用のxcode 4.3バージョンでcoredataプロジェクトに取り組んでいます。CoreData チェックボックスが選択された ARC の空のプロジェクトを作成しました。モデルを管理するために xcode によって追加されたメソッドに従い、事前設定された sqllite db をアプリにコピーするためのチュートリアルに従って変更されました。事前設定された db はプロジェクト フォルダー内のリソース グループにあります。モデルとプロジェクトの名前が同じです。
- (void)saveContext
{
NSError *error = nil;
NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
if (managedObjectContext != nil) {
if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
}
#pragma mark - Core Data stack
// Returns the managed object context for the application.
// If the context doesn't already exist, it is created and bound to the persistent store coordinator for the application.
- (NSManagedObjectContext *)managedObjectContext
{
if (_managedObjectContext != nil) {
return _managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
_managedObjectContext = [[NSManagedObjectContext alloc] init];
[_managedObjectContext setPersistentStoreCoordinator:coordinator];
}
return _managedObjectContext;
}
// Returns the managed object model for the application.
// If the model doesn't already exist, it is created from the application's model.
- (NSManagedObjectModel *)managedObjectModel
{
if (_managedObjectModel != nil) {
return _managedObjectModel;
}
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"Prova1" withExtension:@"momd"];
NSLog(@"modelURL %@",modelURL);
_managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
return _managedObjectModel;
}
// Returns the persistent store coordinator for the application.
// If the coordinator doesn't already exist, it is created and the application's store added to it.
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (_persistentStoreCoordinator != nil) {
return _persistentStoreCoordinator;
}
NSString *storePath = [[[self applicationDocumentsDirectory] absoluteString] stringByAppendingPathComponent:@"Prova1.sqlite"];
NSLog(@"storePath %@",storePath);
NSURL *storeURL = [NSURL fileURLWithPath:storePath];
NSLog(@"storeURL %@",storeURL);
// Put down default db if it doesn't already exist
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:storePath]) {
NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"Prova1" ofType:@"sqlite"];
if (defaultStorePath) {
[fileManager copyItemAtPath:defaultStorePath toPath:storePath error:NULL];
}
}/**/
/*NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"Prova1.sqlite"];*/
NSError *error = nil;
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
/*
Replace this implementation with code to handle the error appropriately.
abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
Typical reasons for an error here include:
* The persistent store is not accessible;
* The schema for the persistent store is incompatible with current managed object model.
Check the error message to determine what the actual problem was.
If the persistent store is not accessible, there is typically something wrong with the file path. Often, a file URL is pointing into the application's resources directory instead of a writeable directory.
If you encounter schema incompatibility errors during development, you can reduce their frequency by:
* Simply deleting the existing store:
[[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil]
* Performing automatic lightweight migration by passing the following dictionary as the options parameter:
@{NSMigratePersistentStoresAutomaticallyOption:@YES, NSInferMappingModelAutomaticallyOption:@YES}
Lightweight migration will only work for a limited set of schema changes; consult "Core Data Model Versioning and Data Migration Programming Guide" for details.
*/
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return _persistentStoreCoordinator;
}
#pragma mark - Application's Documents directory
// Returns the URL to the application's Documents directory.
- (NSURL *)applicationDocumentsDirectory
{
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}
ここでは代わりに、db と対話するためのいくつかのフィールドを使用したビューコントローラーの実装です。
@implementation ALCViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.textfied1 = [[UITextField alloc] initWithFrame:CGRectMake(200, 50, 300, 30)];
self.textfied1.borderStyle = UITextBorderStyleRoundedRect;
self.textfied1.delegate = self;
[self.view addSubview:self.textfied1];
self.textfied2 = [[UITextField alloc] initWithFrame:CGRectMake(200, 100, 300, 30)];
self.textfied2.borderStyle = UITextBorderStyleRoundedRect;
self.textfied2.delegate = self;
[self.view addSubview:self.textfied2];
self.salvaButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[self.salvaButton setFrame:CGRectMake(200, 150, 100, 20)];
[self.salvaButton setTitle:@"salva" forState:UIControlStateNormal];
[self.salvaButton addTarget:self action:@selector(salva:) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:self.salvaButton];
self.cercaButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[self.cercaButton setFrame:CGRectMake(200, 200, 100, 20)];
[self.cercaButton setTitle:@"Elenca" forState:UIControlStateNormal];
[self.cercaButton addTarget:self action:@selector(cerca:) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:self.cercaButton];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
UIToolbar *toolbar = [[UIToolbar alloc] init];
[toolbar setBarStyle:UIBarStyleBlackTranslucent];
[toolbar sizeToFit];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(doneClicked:)];
if ([textField isEqual:self.textfied1]) {
[doneButton setTag:1];
}
if ([textField isEqual:self.textfied2]) {
[doneButton setTag:2];
}
[toolbar setItems:[NSArray arrayWithObjects:/*flexibleSpaceLeft,*/ doneButton, nil]];
[textField setInputAccessoryView:toolbar];
[textField setText:@""];
[textField setTextColor:[UIColor blackColor]];
[textField setFont:[UIFont systemFontOfSize:16]];/**/
return YES;
}
- (void)doneClicked:(id)sender {
UIButton *bSender = (UIButton *)sender;
switch ([bSender tag]) {
case 1: {
[self.textfied1 resignFirstResponder];
}
break;
case 2: {
[self.textfied2 resignFirstResponder];
}
break;
default:
break;
}
}
- (void)salva:(id)sender {
ALCAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSManagedObject *newContact;
newContact = [NSEntityDescription insertNewObjectForEntityForName:@"Stica" inManagedObjectContext:context];
[newContact setValue:self.textfied1.text forKey:@"nome"];
[newContact setValue:self.textfied2.text forKey:@"sfiga"];
self.textfied1.text = @"";
self.textfied2.text = @"";
NSError *error;
[context save:&error];
//status.text = @”Contact saved”;
}
- (void)cerca:(id)sender {
ALCAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSEntityDescription *entityDesc = [NSEntityDescription entityForName:@"Stica" inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDesc];
/*NSPredicate *pred = [NSPredicate predicateWithFormat:@"(name = %@)", name.text];
[request setPredicate:pred];*/
NSError *error;
NSArray *objects = [context executeFetchRequest:request error:&error];
if ([objects count] == 0) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Risultati" message:@"Vuoto" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
}
else {
for (NSManagedObject *object in objects) {
NSMutableString *string = [[NSMutableString alloc] initWithFormat:@"%@ - %@",[object valueForKey:@"nome"],[object valueForKey:@"sfiga"]];
NSLog(@"%@",string);
}
}
}
@end
データベース内に保存されているレコードを表示しようとすると問題が発生します。実際、「Elenca」という名前のボタンを押すとエラーが発生し、xcode で次のエラーが表示されます。
Unresolved error Error Domain=NSCocoaErrorDomain Code=512
"The operation couldn’t be completed. (Cocoa error 512.)"
UserInfo=0x6e7a120 {
reason=Failed to create file; code = 2
},
{
reason = "Failed to create file; code = 2";
}
いくつかの変数も出力しようとしました:
storePathファイル:/localhost/Users/winimac01/Library/Application%20Support/iPhone%20Simulator/5.1/Applications/7733DC6C-CE52-4EB4-9A60-26962F7AEDD9/Documents/Prova1.sqlite
storeURL file:/localhost/Users/winimac01/Library/Application%2520Support/iPhone%2520Simulator/5.1/Applications/7733DC6C-CE52-4EB4-9A60-26962F7AEDD9/Documents/Prova1.sqlite -- file://localhost/
モデルURLattributeType 700 , attributeValueClassName NSString, defaultValue (null)\";\n nome = \"(), name nome, isOptional 1, isTransient 0, entity Meco, renamingIdentifier nome, 検証述語 (\n), 警告 (\n), versionHashModifier (null)\n userInfo {\n}, attributeType 700 , attributeValueClassName NSString, defaultValue (null)\";\n 関係 = \"(), 名前関係, isOptional 1, isTransient 0, エンティティ Meco, renamingIdentifier 関係, 検証述語 (\n)、警告 (\n)、versionHashModifier (null)\n userInfo {\n}、宛先エンティティ Stica、inverseRelationship リレーションシップ、minCount 1、maxCount 1、isOrdered 0、deleteRule 1\";\n}、サブエンティティ(null), userInfo {\n}, versionHashModifier (null)"; Stica = "() name Stica, managedObjectClassName NSManagedObject, renamingIdentifier Stica, attributeType 700 、attributeValueClassName NSString、defaultValue (null)\";\n}、サブエンティティ (null)、userInfo {\n}、versionHashModifier (null)"; }、要求テンプレートをフェッチ { }
また、 storeURLがその形式になっている理由がわかりません。文字列の末尾にある " -- file://localhost/" は何ですか? .sqlite ファイルを作成できないのはなぜですか? 問題を解決する正しい方法はどれですか?私はすでにシミュレーターをリセットしようとしましたが、パスは正しいです...
前もって感謝します
edit1: 私が勉強したツットはこれです
edit2: 機能 する可能性のある解決策は、次のコードを変更することです。
NSString *storePath = [[[self applicationDocumentsDirectory] absoluteString] stringByAppendingPathComponent:@"Prova1.sqlite"];
NSURL *storeURL = [NSURL fileURLWithPath:storePath];
// Put down default db if it doesn't already exist
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:storePath]) {
NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"Prova1" ofType:@"sqlite"];
if (defaultStorePath) {
[fileManager copyItemAtPath:defaultStorePath toPath:storePath error:NULL];
}
}/**/
と:
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"Prova1.sqlite"];
// Put down default db if it doesn't already exist
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:storePath]) {
NSURL *defaultStoreURL = [[NSBundle mainBundle] URLForResource:@"Prova1" withExtension:@"sqlite"];
if (defaultStoreURL) {
[fileManager copyItemAtURL:defaultStoreURL toURL:storeURL error:NULL];
}
}/**/