insertNewObjectForEntityForName:

前端 未结 3 1657
小蘑菇
小蘑菇 2020-12-01 19:24

I set up an Entity using the Xcode .xcdatamodel file editor. I created an entity called Person, added a few attributes, then generated a .m file to represent it. That all

3条回答
  •  心在旅途
    2020-12-01 19:48

    You need the init of the Core Data

    -(void)initCoreData{
            NSError *error;
            //Path to sqlite file.
            NSString *path = [NSHomeDirectory() stringByAppendingString:@"/Documents/Level4.sqlite"];
            NSURL *url = [NSURL fileURLWithPath:path];
    
            //init the model
            NSManagedObjectModel *managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
    
            //Establish the persistent store coordinator
            NSPersistentStoreCoordinator *persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:managedObjectModel];
    
            if(![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:url options:nil error:&error]){
    
                    NSLog(@"Error %@",[error localizedDescription]);
    
            }else{
                    self.context = [[[NSManagedObjectContext alloc ] init ] autorelease];
                    [self.context setPersistentStoreCoordinator:persistentStoreCoordinator];
            }
    
            [persistentStoreCoordinator release];
    }
    

提交回复
热议问题