ios - 如何快速创建 Core Data 数据库 4
<p><p>请分享一些用于数据库保存详细信息的示例代码片段,并在 swift 4 中获取保存的详细信息</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>这是我的答案</p>
<p>第 1 步:创建 Coredata 堆栈类,见下文</p>
<p><code>var coreDataStack: CoreDataStack = CoreDataStack()</code>//在 appDelegate 上添加这一行</p>
<pre><code> class Stack
{
lazy var managedObjectModel: NSManagedObjectModel = {
let modelUrl = Bundle.main.url(forResource: "Model", withExtension: "momd")!
return NSManagedObjectModel(contentsOf: modelUrl)!
}()
lazy var peristentStoreCoordinator: NSPersistentStoreCoordinator = {
let coordinator: NSPersistentStoreCoordinator = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)
let applicationDocumentsDirectory: URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).last!
let persistentStoreUrl: URL = applicationDocumentsDirectory.appendingPathComponent("Model.sqlite")
do {
try coordinator.addPersistentStore(ofType: NSSQLiteStoreType, configurationName: nil, at: persistentStoreUrl, options: nil)
}
catch {
fatalError("Persistent store error! \(error)")
}
return coordinator
}()
lazy var managedObjectContext: NSManagedObjectContext = {
let managedObjectContext: NSManagedObjectContext = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)
managedObjectContext.persistentStoreCoordinator = self.peristentStoreCoordinator
return managedObjectContext
}()
func saveContext() {
if self.managedObjectContext.hasChanges {
do {
try self.managedObjectContext.save()
}
catch {
fatalError("There was an error saving the managed object context \(error)")
}
}
}
}
</code></pre>
<hr/>
<p>第 2 步:创建实体
第 3 步:创建 ManagedObjectModelSubclass -- 编辑器 -> ManagedObjectModelSubclass
第四步:导入类的核心数据并添加以下代码</p>
<pre><code> var person = () // here denote subclassclass name
varappDelegate = UIApplication.shared.delegate as! AppDelegate
let personEntity: NSEntityDescription? = NSEntityDescription.entity(forEntityName: "EntityName", in: self.appDelegate.coreDataStack.managedObjectContext)
if personEntity != nil {
let person1: Person = Person(entity: personEntity!, insertInto: self.appDelegate.coreDataStack.managedObjectContext)
person1.name = "name"
person1.country = "US"
person1.age = "26"
person1.place = "NewYork"
}
</code></pre>
<p>第 5 步:使用以下代码获取数据</p>
<pre><code>var persons: = [] // subclass name
var appDelegate: AppDelegate = UIApplication.shared.delegate as! AppDelegate
var listdata = () // create a modelclass object
func fetchDemoData() {
let fetchRequest: NSFetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "EntityName")
do {
if let results = try self.appDelegate.coreDataStack.managedObjectContext.fetch(fetchRequest) as? {
let personList: ? = results as?
if personList != nil {
self.persons = personList!
for eachData in persons {
let name = eachData.name as String!
let username = eachData.age as String!
let email = eachData.country as String!
let address = eachData.place as!
self.listdata.append(ModelClass(name: name!, username: username!,email : email!,address : address))
}
//self.tableview.reloadData()
}
}
}
catch {
fatalError("There was an error fetching the items")
}
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - 如何快速创建 Core Data 数据库 4,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/47671215/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/47671215/
</a>
</p>
页:
[1]