0
votes

I have an array controller which is bound to an nstableview. I also have some nstextfields which the user populates then presses an "add" button. I want to take those fields, first_name and last_name, and use them to populate an entity. I'll call the entity PersonEntity.

So in the delegate for the add button I get the string values for the 2 text fields, populate an entity, then add it to the array controller. I'm new to cocoa/objective-c. This seems like a straightforward thing but it appears that I cannot create an entity like I expect

PersonEntity* person
[person setFirst_name:firstName];
[person setLast_name:lastName];
[customerArray addObject:person];

It crashes saying I can't add nil at the [customerArray addObject:customer] line. That line is my attempt to add the entity to the array controller which is bound to the tableview. What is the correct way to do something like this?

1
Can you post a bit more code for context? What type of object is customer? Is person ever allocated? - sbooth
sbooth, I fixed the code there shouldn't have been a customer. Person is not allocated, it crashes if I do an alloc/init on person. - JonF
If person hasn't been allocated, that is likely the problem. If PersonEntity *person = [[PersonEntity alloc] init] crashes then there is another source of the problem. What is PersonEntity's superclass? - sbooth
NSManagedObject, it was created from a core data entity I made in the gui editor. I did something in Xcode to generate the class. I believe I went to Editor->Create NSManagedObject subclass after clicking on the entity. I'm not 100% sure that's the menu option I used, but i don't see anything else that I might have used - JonF

1 Answers

3
votes

I'm not sure if PersonEntity is a Core Data Entity, but since you question is also tagged as Core Data, I'll assume it is.

If your ArrayController (the one bound to your NSTableView), is bound to CoreData source, you don't add objects directly to it. Instead you add it to your managedObjectContext and it will reflect on your NSTableView.

The code should look like this:

 PersonEntity *person = [NSEntityDescription insertNewObjectForEntityForName:@"PersonEntity"
                                        inManagedObjectContext: managedObjectContext];

[person setFirst_name:firstName];
[person setLast_name:lastName];

/* Save Event */
NSError * error = nil;
[__managedObjectContext save: &error];

Hope this helps! Mane