Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var setting: [NSManagedObject] = []
- var managedContext: NSManagedObjectContext
- var appDelegate: AppDelegate
- override init() {
- self.appDelegate = (UIApplication.shared.delegate as? AppDelegate)!
- self.managedContext = self.appDelegate.persistentContainer.viewContext
- super.init()
- }
- static var instance = DB()
- func userSaveValue(key: String, value: String) {
- // Описание сущности
- let entityDescription = NSEntityDescription.entity(forEntityName: "User", in: self.managedContext)
- // Создание нового объекта
- let managedObject = NSManagedObject(entity: entityDescription!, insertInto: self.managedContext)
- // Установка значения атрибута
- managedObject.setValue(value, forKey: key)
- // Извлечение значения атрибута
- let name = managedObject.value(forKey: key)
- print("status = (name)")
- // Запись объекта
- self.saveContext()
- }
- func userGetValue(key: String) -> String {
- // Описание сущности
- let entityDescription = NSEntityDescription.entity(forEntityName: "Гыук", in: self.managedObjectContext)
- // Создание нового объекта
- let managedObject = NSManagedObject(entity: entityDescription!, insertInto: self.managedObjectContext)
- // Извлечение значения атрибута
- let name = managedObject.value(forKey: key) as! String
- print("name = (name)")
- return name
- }
- func saveContext () {
- if managedContext.hasChanges {
- do {
- try managedContext.save()
- } catch {
- let nserror = error as NSError
- NSLog("Unresolved error (nserror), (nserror.userInfo)")
- abort()
- }
- }
- }
- }
- class AppDelegate: UIResponder, UIApplicationDelegate {
- lazy var persistentContainer: NSPersistentContainer = {
- let container = NSPersistentContainer(name: "QalaData")
- container.loadPersistentStores { description, error in
- if let error = error {
- fatalError("Unable to load persistent stores: (error)")
- }
- }
- return container
- }()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement