データを保存して、単純な CoreData タスクを実行しようとしています。Beta 6 では動作するはずですが、Beta 7 にアップデートしてからエラーが発生し始めました。
「?」を追加する必要があると思います。また '!' エラーのヒントに基づいていますが、どこにあるかを理解するほど賢くありません!
@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)
エラーをクリアして正常にコンパイルされたように見えるたびに、「保存」をクリックするとデバッグ領域に表示されます
fatal error: unexpectedly found nil while unwrapping an Optional value