Неизменный объект сбоя ядра данных

Почему этот сбой?

CategoryOfExpense *newCatEx = (CategoryOfExpense *) [NSEntityDescription entityForName:kCategoryOfExpense inManagedObjectContext:moc];
newCatEx.name = self.nameTextField.text; <- crashes here
newCatEx.icon =  [NSData dataWithData:UIImagePNGRepresentation(self.iconImageView.image)];

Ошибка:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Can't modify an immutable model.'

3 ответа

Решение

Ты звонишь entityForName:inManagedObjectContext: который возвращает NSEntityDescription, Это будет определение сущности, например, как вы описали ее в графическом интерфейсе при создании модели.

Я верю что ты хочешь insertNewObjectForEntityForName:inManagedObjectContext: который создаст новый NSManagedObject сущности.

Ты делаешь это неправильно. Это с сайта разработчика:

Редактирование описания сущностей:

Entity descriptions are editable until they are used by an object graph manager.
This allows you to create or modify them dynamically. 
However, once a description is used (when the managed object model to which it belongs 
is associated with a persistent store coordinator), it must not (indeed cannot) be changed.
This is enforced at runtime: any attempt to mutate a model or any of its sub-objects 
after the model is associated with a persistent store coordinator causes an exception to 
be thrown. If you need to modify a model that is in use, create a copy, modify the copy, 
and then discard the objects with the old model.

Исключение, упомянутое ближе к концу, это то, что вы получаете. Далее к концу это то, что вам нужно сделать.

Попробуй это:

Вы можете получить изменяемую копию модели, позвонив -mutableCopy на NSManagedObjectModel пример.

Нашел здесь Как редактировать NSManagedObjectModel, который был загружен из momd в iOS5

Другие вопросы по тегам