View Full Version : Efficiently implementing simple NSComboBox functionality with bindings

Jul 4, 2011, 04:42 AM
I made a sample core data application, in which I populated NSComboBox with name of Student entity, by binding its content values property to an array controller. I wanted to add new record to persistent store if student name does not exist, so I defined this method in application delegate class:

- (IBAction)addNewStudent:(id)sender
NSArray *studentNames = [[students arrangedObjects] valueForKey:@"name"];
if ([studentNames containsObject:[sender stringValue]]) {

NSManagedObjectContext *context = [self managedObjectContext];
NSEntityDescription *studentEntity = [NSEntityDescription entityForName:@"Student" inManagedObjectContext:context];

// inserting record
NSManagedObject *newRecord = [NSEntityDescription insertNewObjectForEntityForName:[studentEntity name] inManagedObjectContext:context];
[newRecord setValue:[sender stringValue] forKey:@"name"];

NSError *error = nil;
if (![context save:&error]) {
NSLog(@"Unresolved error %@, %@",error,[error userInfo]);

Can anyone suggest me, if it is the best way to implement it or there is any other better approach to implement it?