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模拟器文件夹....

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 回答