请分享一些用于数据库保存详细信息的示例代码片段,并在 swift 4 中获取保存的详细信息
这是我的答案
第 1 步:创建 Coredata 堆栈类,见下文
var coreDataStack: CoreDataStack = CoreDataStack()
//在 appDelegate 上添加这一行
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("ersistent 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)")
}
}
}
}
第 2 步:创建实体 第 3 步:创建 ManagedObjectModelSubclass -- 编辑器 -> ManagedObjectModelSubclass 第四步:导入类的核心数据并添加以下代码
var person = [Person]() //[Person] here denote subclassclass name
var appDelegate = 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"
}
第 5 步:使用以下代码获取数据
var persons: [Person] = [] // subclass name
var appDelegate: AppDelegate = UIApplication.shared.delegate as! AppDelegate
var listdata = [ModelClass]() // 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? [NSManagedObject] {
let personList: [Person]? = results as? [Person]
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! [String : Any]
self.listdata.append(ModelClass(name: name!, username: username!,email : email!,address : address))
}
//self.tableview.reloadData()
}
}
}
catch {
fatalError("There was an error fetching the items")
}
}
关于ios - 如何快速创建 Core Data 数据库 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47671215/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |