Why does this crash?
CategoryOfExpense *newCatEx = (CategoryOfExpense *) [NSEntityDescription entityForName:kCategoryOfExpense inManagedObjectContext:moc];
newCatEx.name = self.nameTextField.text; <- crashes here
newCatEx.icon = [NSData dataWithData:UIImagePNGRepresentation(self.iconImageView.image)];
The error is:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Can't modify an immutable model.'
You are calling
entityForName:inManagedObjectContext:
which returns aNSEntityDescription
. That would be the definition of the entity, like how you described it in the GUI when you built the model.I believe what you want is
insertNewObjectForEntityForName:inManagedObjectContext:
which will create a newNSManagedObject
of the entity.