override func viewWillDisappear(animated: Bool) {
// need to add a device?
if profile == nil {
if let name = nameTextField.text, species = speciesTextField.text, dob: NSDate = datePicker.date, entity = NSEntityDescription.entityForName("Profile", inManagedObjectContext: managedObjectContext) where !name.isEmpty && !species.isEmpty {
profile = Profile(entity: entity, insertIntoManagedObjectContext: managedObjectContext)
profile?.name = name
profile?.species = species
profile?.dob = dob
}
}
}
每當我跑我的應用程序,這部分代碼會產生「致命錯誤:意外發現零而展開的可選值」。我確定名稱,物種和dob確實包含控制檯中顯示的數據。你知道什麼變量可以包含零嗎?我正在使用核心數據,並且不熟悉它。CoreData斯威夫特:致命錯誤:意外發現零而展開的可選值
屏幕:
實體:
你看過調試區域並檢查哪個變量爲零嗎?只是好奇,你能展示你的實體的截圖嗎? – Eendje
我想我已經找到了它,它是你的'managedObjectContext'。你隱含地解開它並導致它崩潰。 – Eendje
嗨,感謝您的回覆。我看不到在調試屏幕上沒有任何變量(請參閱更新後的圖像)。 – Charris11