3

なぜこれがクラッシュするのですか?

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.'
4

3 に答える 3

7

entityForName:inManagedObjectContext:を返す呼び出していますNSEntityDescription。これは、モデルを構築したときにGUIで説明したように、エンティティの定義になります。

私はあなたが望むものは、エンティティinsertNewObjectForEntityForName:inManagedObjectContext:の新しいものを作成することだと信じています。NSManagedObject

于 2012-09-07T20:24:33.297 に答える
1

あなたはそれを間違っています。これは開発サイトからのものです:

エンティティの説明の編集:

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.

最後に言及されている例外は、あなたが得るものです。さらに終わりに向かって、あなたがする必要があることです。

于 2012-09-07T19:12:45.173 に答える
0

これを試して:

インスタンスを呼び出し-mutableCopyて、モデルの変更可能なコピーを取得できます。NSManagedObjectModel

ここにあります iOS5 で momd からロードされた NSManagedObjectModel を編集する方法

于 2012-09-07T18:45:12.963 に答える