我一直在嘗試做一個簡單的CoreData任務,保存數據。我確定它可以在Beta 6中運行,但在更新到Beta 7後開始出現錯誤。Beta 7中的XCode 6 Beta 6錯誤 - 可選類型的值未解包
我想我必須添加'?'要麼 '!'基於錯誤提示,但只是不夠聰明,弄清楚哪裏!
@IBAction func saveItem(sender: AnyObject) {
// Reference to App Delegate
let appDel: AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
// Reference our moc (managed object content)
let contxt: NSManagedObjectContext = appDel.managedObjectContext!
let ent = NSEntityDescription.entityForName("List", inManagedObjectContext: contxt)
// Create instance of our data model and initialize
var newItem = Model(entity: ent, insertIntoManagedObjectContext: contxt)
// Map our attributes
newItem.item = textFieldItem.text
newItem.quanitity = textFieldQuantity.text
newItem.info = textFieldInfo.text
// Save context
contxt.save(nil)
}
錯誤說
Value of optional type 'NSEntityDescription?' not unwrapped; did you mean to use '!' or '?'
在生產線
var newItem = Model(entity: ent, insertIntoManagedObjectContext: contxt)
每次我似乎有明顯的錯誤,並編譯OK,點擊「保存」顯示了在調試區
fatal error: unexpectedly found nil while unwrapping an Optional value