Hi just wondering if this is possible, i have a simple counter app where a counter variable is incremented by 1 every time a button is pushed. is it possible to save the counter when the user exits the app using core data? i know NSUserdefaults would work here but im exploring core data and was wondering if it could be used in cases like this
class ViewController: UIViewController {
var counter = Int()
@IBOutlet var label: UILabel!
@IBAction func button(sender: AnyObject) {
counter += 1
label.text = "\(counter)"
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
}
NSManagedObject subclass Error
class IntegerEntity: NSManagedObject {
convenience init(entity: NSEntityDescription, insertIntoManagedObjectContext context: NSManagedObjectContext?, value: Int) {
self.init(entity: entity, insertIntoManagedObjectContext: context)
self.value
// ERROR value of type IntegerEntity has no member value
}
}
ViewController.swift errors
import UIKit
import CoreData
class ViewController: UIViewController {
var counter = Int()
@IBOutlet var label: UILabel!
@IBAction func button(sender: AnyObject) {
counter += 1
label.text = "\(counter)"
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
let dataContext: NSManagedObjectContext! = (UIApplication.sharedApplication().delegate as? AppDelegate)?.managedObjectContext
var integerEntity: IntegerEntity!
if dataContext != nil {
let entity = NSEntityDescription.entityForName("IntegerEntity", inManagedObjectContext: dataContext)
let request = NSFetchRequest()
request.entity = entity
let integer = try? dataContext.executeFetchRequest(request)
if integer != nil && !integer!.isEmpty {
(integer?.first! as! IntegerEntity).value = counter
// ERROR value of type IntegerEntity has no member value
} else {
let newInt = IntegerEntity(entity: entity, insertIntoManagedObjectContext: context, value: counter)
// ERROR Use of unresolved identifier context
dataContext.saveData()
}
}
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
let dataContext: NSManagedObjectContext! = (UIApplication.sharedApplication().delegate as? AppDelegate)?.managedObjectContext
var integerEntity: IntegerEntity!
if dataContext != nil {
let entity = NSEntityDescription.entityForName("IntegerEntity", inManagedObjectContext: dataContext)
let request = NSFetchRequest()
request.entity = entity
let integer = try? dataContext.executeFetchRequest(request)
if integer != nil && !integer!.isEmpty {
counter = (integer!.first! as! IntegerEntity).value
// ERROR value of type IntegerEntity has no member value
} else {
counter = 0
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
extension NSManagedObjectContext {
func saveData() -> Bool {
do {
try self.save()
return true
} catch let error as NSError {
print(error)
return false;
}
}
}
do - catchand handle errors properly. On success you get a guaranteed non-optional array. And theNSManagedObjectContextobject is supposed to be non-optional, too. And how canAppDelegateever benil?? If there is no application delegate the app won't launch at all.