嗨,只是想知道這是否可能,我有一個簡單的計數器應用程序,每次按下按鈕時,計數器變量都會加1。當用戶使用核心數據退出應用程序時可以保存計數器嗎?我知道會NSUserDefaults的在這裏工作,但IM探索的核心數據,並想知道它是否能在情況下可以使用這樣使用核心數據來保存一個不斷變化的整數
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子類錯誤
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錯誤
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;
}
}
}
是的,當然。然而,這是一個非常糟糕的匹配任務。這就像啓動一個747穿過街道到商店買一瓶牛奶。核心數據功能非常強大,但學習曲線陡峭,開銷相當大。用它來存儲一個整數是愚蠢的。 –
使用'do-catch'並正確處理錯誤。成功後,您將獲得一個有保證的非可選數組。而NSManagedObjectContext對象也應該是非可選的。而且'AppDelegate'怎麼可能是'nil'?如果沒有應用程序委託,該應用程序將不會啓動。 – vadian