2016-02-03 32 views
0
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斯威夫特:致命錯誤:意外發現零而展開的可選值

屏幕: enter image description here

實體: enter image description here

+0

你看過調試區域並檢查哪個變量爲零嗎?只是好奇,你能展示你的實體的截圖嗎? – Eendje

+0

我想我已經找到了它,它是你的'managedObjectContext'。你隱含地解開它並導致它崩潰。 – Eendje

+0

嗨,感謝您的回覆。我看不到在調試屏幕上沒有任何變量(請參閱更新後的圖像)。 – Charris11

回答

0

在你的屏幕截圖,它說:

managedObjectContext = (NSManagedObjectContext!)nil 

所以最有可能的,這是你的崩潰的原因。

+0

這一定是,謝謝。我認爲managedObjectContext會在我創建實體時進行初始化。回到我認爲的文檔。 – Charris11

0

非常好可能產生零值文本字段之一。輸入一箇中斷點並檢查它們(和datePicker)爲零。你正在使用IBOutlets,還是以編程方式創建的?

+0

嗨,感謝您的回覆。我用IBOutlets。正如你從屏幕截圖(上面更新)中看到的那樣,調試器將文本字段顯示爲具有值。 – Charris11

相關問題