以编程方式使用ios-core数据创建新表

she*_*law 6 database sqlite iphone core-data ios

Core Data是否允许我以编程方式创建新表?或者如果我需要,我需要直接使用SQLite.

谢谢

Tim*_*ean 4

从 CoreData 的角度来看,您实际上并没有创建新表,因为数据库表只是与核心数据模型关联的持久性存储的一种可能类型。

但是,您可以使用 NSEntityDescription 类以编程方式创建新的核心数据实体。在 NSEntityDescription 类文档中,您会发现:

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.
Run Code Online (Sandbox Code Playgroud)

我从未尝试在运行时修改它,因此我不确定当您拥有现有的 SQLite 持久性存储(如果有的话)时,它的效果到底如何。但也许值得尝试一下 NSEntityDescription 来看看它是否能让您接近您想要做的事情。