1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

Efficiently implementing simple NSComboBox functionality with bindings

Discussion in 'Mac Programming' started by Monaj, Jul 4, 2011.

  1. Monaj, Jul 4, 2011
    Last edited: Jul 4, 2011

    macrumors regular

    #1
    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:

    Code:
    - (IBAction)addNewStudent:(id)sender
        {
            NSArray *studentNames = [[students arrangedObjects] valueForKey:@"name"];
            if ([studentNames containsObject:[sender stringValue]]) {
                return;
            }
            
            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]);
                abort();
            }
        }
    Can anyone suggest me, if it is the best way to implement it or there is any other better approach to implement it?
     

Share This Page