0

私はiosが初めてです。私はiosを学ぼうとしていますが、今までiosでCRUDを行うことに成功していません.

私はチュートリアルに従っていましたが、コードに問題はなく、以下のコードが表示されます。

  1. コア データと ARC を含む空のアプリケーションを使用します。
  2. Person という名前のエンティティを作成します (人気のあるチュートリアルの例: www.youtube.com/watch?v=bC3F8a4F_KE)
  3. 次に、NSObject名前付き IMSPerson のサブクラスを追加しました。
  4. のサブクラスを追加しましUIVIEWCONTROLLERた。
  5. 次に、btnSavePerson で挿入メソッドを作成します。

主な問題は、データが挿入されていることを示すCORE DATA dbファイルがないことです...

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
    if (_persistentStoreCoordinator != nil) {
        return _persistentStoreCoordinator;
    }

    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"CrudWithInterface.sqlite"];

このファイルの場所 CrudWithInterface.sqlite

保存ボタンのイベント

- (IBAction)btnAddPerson:(id)sender {
    NSEntityDescription *entityDesc = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:context];
    NSManagedObject *newPerson = [[NSManagedObject alloc]initWithEntity:entityDesc insertIntoManagedObjectContext:context];
    [newPerson setValue:self.txtFirstName.text forKey:@"firstName"];
    [newPerson setValue:self.txtFirstName.text forKey:@"lastName"];
    [newPerson setValue:self.txtFirstName.text forKey:@"address"];

    NSError *error;
    [context save:&error];

    self.fldDisplayData.text = @"person Added Successfully... :)";

}

~/User/Library/SharedAllication..... だったパスも検索しました。

そして、その中にIOS Simulator Folderはありません....

4

1 に答える 1

0

これを試して :

ステップ 1 : 永続ストア コーディネーターのすべてのコードが表示されません。アプリをさらに実行する場合は、NSPersistentStoreCoordinator を読み取るか作成する必要があります。

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
    if (_persistentStoreCoordinator != nil) {
        return _persistentStoreCoordinator;
    }

    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"CrudWithInterface.sqlite"];

    NSError *error = nil;
    _persistentStoreCoordinator = [[[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:<your managed Obbject Model>] retain];

    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];

    if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error])
    {
        NSLog(@"Suppression du model de données !!");
        [[NSFileManager defaultManager] removeItemAtURL:storeURL error:NULL];

        [_persistentStoreCoordinator release];
        _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:<your managed Obbject Model>];
        if (![singleton addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error])
        {
            abort();
        }
    }

    return _persistentStoreCoordinator;

}

ステップ2:保存ボタン(コンテキストという名前のmanagedObjectContextをどのように作成したかはわかりません)で、静的メソッド「NSEntityDescription insertNewObjectForEntityForName」を実行できます。これはより便利です

    - (IBAction)btnAddPerson:(id)sender {

        //Normaly you subclass NSManagedObject. On your modele file Editor menu "Create NSManaged subclass" 

        NSManagedObject *newPerson = [NSEntityDescription insertNewObjectForEntityForName:@"Person"                                                       
inManagedObjectContext: context];
        [newPerson setValue:self.txtFirstName.text forKey:@"firstName"];
        [newPerson setValue:self.txtFirstName.text forKey:@"lastName"];
        [newPerson setValue:self.txtFirstName.text forKey:@"address"];

        NSError *error;
        if(![context save:&error])
        {
            DLog(@"Whoops, couldn't save: %@", [err localizedDescription]);
        }

        self.fldDisplayData.text = @"person Added Successfully... :)";


    }
于 2013-09-20T21:22:37.953 に答える