ios – 如何通过CoreData模型创建一个新的实体(表)?

ios – 如何通过CoreData模型创建一个新的实体(表)?,第1张

概述我想在SQLite中创建一个新的实体(表).我的代码如下: +(BOOL)CreateDataSet:(NSManagedObjectModel *) model attributes:(NSDictionary*)attributes entityName:(NSString*) entityName { NSEntityDescription *entityD 我想在sqlite中创建一个新的实体(表).我的代码如下:
+(BOol)CreateDataSet:(NSManagedobjectModel *) model      attributes:(NSDictionary*)attributes     entityname:(Nsstring*) entityname {     NSEntityDescription *entityDef = [[NSEntityDescription alloc] init];    [entityDef setname:entityname];    [entityDef setManagedobjectClassname:entityname];    [model setEntitIEs:[NSArray arrayWithObject:entityDef]];    NSArray *propertIEs =   [CoreDataHelper CreateAttributes:attributes];    [entityDef setPropertIEs:propertIEs];    [entityDef release];    return TRUE;}

但它会引发错误:

Terminating app due to uncaught exception 'NSInternalinconsistencyException',reason: 'Can't modify an immutable model.'*** Call stack at first throw:(    0   CoreFoundation                      0x01c5abe9 __exceptionPreprocess + 185    1   libobjc.A.dylib                     0x01daf5c2 objc_exception_throw + 47    2   CoreData                            0x0152634a -[NSManagedobjectModel(_NSInternalMethods) _throwIfNotEditable] + 106    3   CoreData                            0x01526904 -[NSManagedobjectModel setEntitIEs:] + 36....

这似乎表明该模型是只读的.

解决方法 From the documentation:(重点是我的)

Managed object models are editable
until they are used by an object graph
manager (a managed object context or a
persistent store coordinator). This
allows you to create or modify them
dynamically. However,once a model is
being used,it must not be changed.
This is enforced at runtime—when the
object manager first fetches data
using a model,the whole of that model
becomes uneditable. Any attempt to
mutate a model or any of its
sub-objects after that point 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.

总结

以上是内存溢出为你收集整理的ios – 如何通过CoreData模型创建一个新的实体(表)?全部内容,希望文章能够帮你解决ios – 如何通过CoreData模型创建一个新的实体(表)?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1114620.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-29
下一篇2022-05-29

发表评论

登录后才能评论

评论列表(0条)

    保存