0

Im using xcode 4 and i've created a CoreData model. I would like to know if its possible to insert data into an entity within xcode.

Please don't tell me the only way to enter data into a model is programmatically.

Cheers

user346443
  • 4,672
  • 15
  • 57
  • 80

3 Answers3

3

Ray Wenderlich provides a tutorial on How to Preload/Import Existing Data using a Python script to populate the database.

His three-part series on Core Data is very informative.

Black Frog
  • 11,595
  • 1
  • 35
  • 66
2

You can not enter data directly with XCode. If you don't want to do this with code you can prepopulate your db. Have a look at this Q&A on SO.

Community
  • 1
  • 1
Nick Weaver
  • 47,228
  • 12
  • 98
  • 108
0

It's a very slow procedure, but you can populate Core Data.

Insert the following code in appDelegate.m

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
   ...

    //test
    //writing data

        NSManagedObjectContext *context = [self managedObjectContext];
        NSManagedObject *model = [NSEntityDescription insertNewObjectForEntityForName:@"Invitados" inManagedObjectContext:context];
        [model setValue:@"Alicia" forKey:@"name"];
        [model setValue:@"Sanchez" forKey:@"firstname"];
        [model setValue:@"Romero" forKey:@"secondname"];


        NSError *error;
        if (![context save:&error]) {
            NSLog(@"Couldn't save: %@", [error localizedDescription]);
        }
        //retrieving data
        // NSManagedObjectContext *context = [self managedObjectContext];
        //NSError *error;

        NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
        NSEntityDescription *entity = [NSEntityDescription  entityForName:@"Invitados" inManagedObjectContext:context];
        [fetchRequest setEntity:entity];

        NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];

        for (NSManagedObject *get in fetchedObjects) {
            NSLog(@"Nombre: %@", [get valueForKey:@"name"]);
            NSLog(@"Apellido 1: %@", [get valueForKey:@"firstname"]);
            NSLog(@"Apellido 2: %@",[get valueForKey:@"secondname"]);
        }
        // End test
Luis
  • 447
  • 1
  • 7
  • 24
  • How would you do this with Cocoa? Cocoa does not have `NSFetchRequestController` but instead uses the `NSArrayController` – Pavan Apr 27 '14 at 06:42